Skip to content

Generator-like Client response #2269

@ngxson

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)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions