CHTTP
Selects how response bodies are returned: Text as cstr, JSON as parsed values, or Buffer as owned binary data. This choice is separate from the request method and headers.
Networking
Make HTTP requests with text, JSON-value, or binary responses.
#include <mc/CHTTP.h>Choose the response representation in the constructor. Configure the request before run() or start(); do not reconfigure an active request.
Transport failures throw CError. HTTP error status codes remain observable through status().
class CHTTPenum Type { Text, JSON, Buffer };
Selects how response bodies are returned: Text as cstr, JSON as parsed values, or Buffer as owned binary data. This choice is separate from the request method and headers.
~CHTTP();
void setURL(const cstr& url);
Sets the request URL while the request is idle. Redirect following is enabled by default.
void setType(const cstr& type);
Sets the HTTP request method, such as GET, POST, or DELETE. Set a Content-Type header with addHeader(); the constructor’s Type controls response decoding.
void addHeader(const cstr& header);
Appends one complete request header line, such as Content-Type: application/json. Set headers before starting the request.
void setTimeout(double dt);
Sets the total transfer timeout in seconds. Zero disables the timeout; negative or non-finite values are rejected.
void setPost(const cstr& data);
void setPost(const std::string& data);
void setPost(const cvar& data);
Sets a copied request body and selects posting behavior. A cvar body is serialized as JSON, but callers must add the appropriate Content-Type header themselves.
void addPost(const cstr& key, const cstr& value);
void addPost(const cstr& key, const char* buf, size_t bytes);
Adds a multipart form field from text or counted bytes. Data is copied during the call; this is distinct from setting one raw body with setPost().
Adds a file as a named multipart field. The path must identify a file and remain readable until the transfer finishes.
void unsafeSSL();
Disables peer certificate and host verification for this request object.
cvar run();
Performs the configured request synchronously and returns the decoded body. Transport and decoding failures raise CError; HTTP error status codes remain available through status().
void start();
Starts the request on a background thread. A second start or configuration mutation is rejected until await() completes.
cvar await();
Joins a request started with start() and returns its decoded body. Asynchronous failures are rethrown here; calling without an active request is an error.
int status();
Returns the last completed HTTP response status, or zero before one is available. It is separate from transport success or failure.
Percent-encodes a string for use as an individual URL component. It does not assemble a URL or encode an entire query structure.