HTTP Client¶
-
class HttpClient¶
Asynchronous HTTP client.
This class can be used to fetch data from different HTTP sources asynchronously.
Internally, it uses libcurl for HTTP handling and libuv for socket and polling management. By default, the default loop is used.
Public Functions
-
HttpClient()¶
Create a new HTTP client.
-
~HttpClient()¶
Destructor of HTTP client.
This function will abort all requests created within this client.
-
inline std::shared_ptr<Request> download(std::string url, Request::HeadersCallback headers_cb, Request::BodyCallback body_cb, Request::ResponseCallback response_cb)¶
Download content from an URL.
This function will create a GET request to download the data from the provided URL.
- Parameters:
url – [in] The URL to use
headers_cb – [in] The function to call when headers have been received
body_cb – [in] The function to call when a body chunk has been received
response_cb – [in] The function to call when full response has been received
- Returns:
a reference to a new request holding the request / response,
nullptrotherwise.
-
inline std::shared_ptr<Request> download(std::string url, Request::HeadersCallback headers_cb, Request::BodyCallback body_cb)¶
Download content from an URL.
This function will create a GET request to download the data from the provided URL.
- Parameters:
url – [in] The URL to use
headers_cb – [in] The function to call when headers have been received
body_cb – [in] The function to call when a body chunk has been received
- Returns:
a reference to a new request holding the request / response,
nullptrotherwise.
-
inline std::shared_ptr<Request> download(std::string url, Request::ResponseCallback cb)¶
Download content from an URL.
This function will create a GET request to download the data from the provided URL.
- Parameters:
url – [in] The URL to use
cb – [in] The function to call when full response has been received
- Returns:
a reference to a new request holding the request / response,
nullptrotherwise.
-
std::shared_ptr<Request> send_request(std::string type, std::string url, std::string body, Request::HeadersCallback headers_cb, Request::BodyCallback body_cb, Request::ResponseCallback response_cb)¶
Send an HTTP request.
This function will create a request to send the specified body and received theresponse data from the provided URL.
- Parameters:
type – [in] The request type
url – [in] The URL to use
body – [in] The request body to send
headers_cb – [in] The function to call when headers have been received
body_cb – [in] The function to call when a body chunk has been received
response_cb – [in] The function to call when full response has been received
- Returns:
a reference to a new request holding the request / response,
nullptrotherwise.
-
std::shared_ptr<Request> create_request(std::string type, std::string url)¶
Create a new HTTP request.
This function will create a request. All settings have to be set manually and the Request::send() function has to be called manually as well.
- Parameters:
type – [in] The request type
url – [in] The URL to use
- Returns:
a reference to a new request holding the request / response,
nullptrotherwise.
-
class Request : public std::enable_shared_from_this<Request>¶
HTTP client request.
This class will hold a unique HTTP request.
Public Types
-
using HeadersCallback = std::function<bool(Request &req, int code, ssize_t size)>¶
Headers received callback.
This function is called when all response headers have been received. The header value can be retrieved with get_header().
- Param req:
[in] The current request
- Param code:
[in] The HTTP response code
- Param size:
[in] The HTTP response body size
- Return:
trueif the response body should be received,falseto abort request.
-
using BodyCallback = std::function<size_t(Request &req, const char *buffer, size_t size)>¶
Body chunk received callback.
This function is called when a chunk of the response body has been received.
A call to pause() can be done if all the data have not been consumed during the call. A subsequent call to resume() will call this function again if more data are available.
- Param req:
[in] The current request
- Param buffer:
[in] The buffer containing the body chunk
- Param size:
[in] The body chunk size in bytes
- Return:
the number of bytes used from buffer.
-
using ResponseCallback = std::function<void(Request &req, int code, const std::string &buffer)>¶
Full response callback.
This function is called when the full response has been received.
- Param req:
[in] The current request
- Param code:
[in] The HTTP response code
- Param buffer:
[in] The buffer containing the response body
Public Functions
-
void set_header(const std::string &key, const std::string &value)¶
Set HTTP request headers.
This function can be used to add headers to the HTTP request.
- Parameters:
key – [in] The HTTP header key
value – [in] The HTTP header value
-
inline void set_body(std::string data)¶
Set HTTP request body.
This function can be used to set the HTTP body.
- Parameters:
data – [in] The HTTP body content
-
bool send()¶
Send the request.
This function will send the request. It can be called from any threads.
- Returns:
trueif the request has been sent,falseotherwise.
-
void abort()¶
Abort the request.
This function will abort the request. It can be called from any threads and the body and/or response callback will be called with invalid code.
-
const char *get_header(const char *key)¶
Get an HTTP response header by key.
- Parameters:
key – [in] The HTTP header key
- Returns:
the value of the HTTP header if found,
nullptrotherwise.
-
void pause()¶
Pause current request.
This function can be used to pause the request. It must be called from one of the callbacks provided to the request.
-
void resume()¶
Resume current request.
This function can be called to unpause a request and can be called from any threads.
-
inline bool is_paused() const¶
Check if the request is paused.
- Returns:
trueif the request is paused,falseotherwise.
-
inline void set_headers_handler(HeadersCallback cb)¶
Set headers received callback.
- Parameters:
cb – [in] The function to call when all headers have been received
-
inline void set_body_handler(BodyCallback cb)¶
Set body chunk received callback.
- Parameters:
cb – [in] The function to call when a new body chunk has heen received
-
inline void set_response_handler(ResponseCallback cb)¶
Set full response callback.
- Parameters:
cb – [in] The function to call when full response has been received
-
using HeadersCallback = std::function<bool(Request &req, int code, ssize_t size)>¶
-
HttpClient()¶