Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set_header/set_status/set_content chaining #1971

Open
vitaly-castLabs opened this issue Nov 5, 2024 · 1 comment
Open

set_header/set_status/set_content chaining #1971

vitaly-castLabs opened this issue Nov 5, 2024 · 1 comment

Comments

@vitaly-castLabs
Copy link

Preface

I often find myself writing code similar to this:

server.Post("/add", [](const httplib::Request& req, httplib::Response& res) {
    res.set_header("Access-Control-Allow-Origin", "*");
    res.set_status(201);
    res.set_content("...", "...");
});

It's ok, but we can do better if those setters returned *this (especially if it's not res but a longer name). This widely used pattern is known as method chaining:

    res.set_header("Access-Control-Allow-Origin", "*").set_status(201).set_content("...", "...");

Looks neater (imo) and only requires adding return *this;

Will you be interested in merging a PR adding this feature? Here's an example:
vitaly-castLabs@6ad63ea

@yhirose
Copy link
Owner

yhirose commented Nov 16, 2024

@vitaly-castLabs sorry for the late reply. Please go ahead to send the pull request, and be sure to include a unit test for the changes. Also could you please to the same to Request::set_header? Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants