Thin HTTP Client wrapper for internal use.
More...
#include <http_client.h>
|
|
using | Headers = std::map<std::string, std::string> |
| | http headers
|
|
using | Params = std::multimap<std::string, std::string> |
| | http params
|
| using | WriteCallback = std::function<bool(const std::string_view&)> |
| | Callback type for streaming response body data.
|
|
| static Response | post (const std::string &url, const Headers &headers, const std::string &body, int timeout_ms=30000, WriteCallback write_cb=nullptr, const std::vector< MultipartFormData > &multipart={}) |
| | Static POST (transient).
|
| static Response | get (const std::string &url, const Headers &headers, int timeout_ms=30000) |
| | Static GET (transient).
|
| static Response | get (const std::string &url, const Params ¶ms, const Headers &headers, int timeout_ms=30000) |
| | Static GET with query parameters (transient).
|
Thin HTTP Client wrapper for internal use.
Usage contract:
- Callers receive an HTTPClient::Response and should inspect response.error and response.status_code to decide success.
- For streaming responses provide a WriteCallback to post() to receive incremental body chunks.
◆ WriteCallback
Callback type for streaming response body data.
The callback receives a string_view containing the next chunk of response body data. It should return true to continue receiving data or false to abort the transfer.
◆ HTTPClient()
| agents::HTTPClient::HTTPClient |
( |
const std::string & | url | ) |
|
|
explicit |
Construct a persistent HTTPClient.
Establishes the base connection context. The TCP connection is maintained alive where possible (Keep-Alive).
◆ Get()
| Response agents::HTTPClient::Get |
( |
const std::string & | url, |
|
|
const Headers & | headers = {}, |
|
|
int | timeout_ms = 30000 ) |
Instance GET (persistent).
- Parameters
-
| url | The URL to GET. |
| headers | The headers to include. |
| timeout_ms | The timeout in milliseconds. |
◆ get() [1/2]
| Response agents::HTTPClient::get |
( |
const std::string & | url, |
|
|
const Headers & | headers, |
|
|
int | timeout_ms = 30000 ) |
|
static |
Static GET (transient).
- Parameters
-
| url | The URL to GET. |
| headers | The headers to include. |
| timeout_ms | The timeout in milliseconds. |
- Returns
- Response The HTTP response.
◆ get() [2/2]
| Response agents::HTTPClient::get |
( |
const std::string & | url, |
|
|
const Params & | params, |
|
|
const Headers & | headers, |
|
|
int | timeout_ms = 30000 ) |
|
static |
Static GET with query parameters (transient).
- Parameters
-
| url | The URL to GET. |
| params | The query parameters. |
| headers | The headers to include. |
| timeout_ms | The timeout in milliseconds. |
- Returns
- Response The HTTP response.
◆ operator=()
Safe move constructor with equals operator.
- Returns
- HTTPClient& Http client object
◆ Post()
| Response agents::HTTPClient::Post |
( |
const std::string & | url, |
|
|
const Headers & | headers, |
|
|
const std::string & | body, |
|
|
int | timeout_ms = 30000, |
|
|
WriteCallback | write_cb = nullptr, |
|
|
const std::vector< MultipartFormData > & | multipart = {} ) |
Instance POST (persistent).
- Parameters
-
| url | The URL to POST to. |
| headers | The headers to include. |
| body | The body to include. |
| timeout_ms | The timeout in milliseconds. |
| write_cb | Optional streaming write callback. |
| multipart | Optional multipart form data. |
◆ post()
| Response agents::HTTPClient::post |
( |
const std::string & | url, |
|
|
const Headers & | headers, |
|
|
const std::string & | body, |
|
|
int | timeout_ms = 30000, |
|
|
WriteCallback | write_cb = nullptr, |
|
|
const std::vector< MultipartFormData > & | multipart = {} ) |
|
static |
Static POST (transient).
- Parameters
-
| url | The URL to POST to. |
| headers | The headers to include. |
| body | The body to include. |
| timeout_ms | The timeout in milliseconds. |
| write_cb | Optional streaming write callback. |
| multipart | Optional multipart form data. |
- Returns
- Response The HTTP response.