Skip to content

Generator-like Client response #2269

Closed
Closed

Description

@ngxson

Hi,

I would request a new feature in the library: allowing to read data from httplib::Client in a generator-like way.

The problem is that currently, to read data progressively, we will need to use callback function. An example can be found in this section: https://github.com/yhirose/cpp-httplib?tab=readme-ov-file#receive-content-with-a-content-receiver-1

std::string body;

auto res = cli.Get("/large-data",
  [&](const char *data, size_t data_length) {
    body.append(data, data_length);
    return true;
  });

What I propose is to instead, allow using res as a generator, for example:

std::string body;

auto res = cli.Get("/large-data");
while (res->has_data()) {
  body.append(res->data(), res->len());
  res->next();
}

This will be useful for the use case where we need to pipe the response back to set_chunked_content_provider in httplib::Server (for example, doing a reverse proxy)

Activity

  1. ngxson commented on Nov 11, 2025

    @ngxson
    Author

    This will be useful for the use case where we need to pipe the response back to set_chunked_content_provider in httplib::Server (for example, doing a reverse proxy)

    Otherwise, if there is a way to just give the Result returned by Client::Get() back to server's Response, I think it can also be a nice solution

  2. yhirose commented on Nov 14, 2025

    @yhirose
    Owner

    @ngxson, thanks for the suggestion! It looks like it would need a lot of changes to httplib.h. I'll check how hard it is to implement when I have time.

  3. yhirose commented on Dec 2, 2025

    @yhirose
    Owner

    @ngxson I implemented your suggestion at #2281. If you have any additional comments, please let me know. Thanks!
    https://github.com/yhirose/cpp-httplib/blob/master/README.md#streaming-api
    https://github.com/yhirose/cpp-httplib/blob/master/README-stream.md

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions