PL::HttpClient class
-
class HttpClient : public PL::Lockable
HTTP/HTTPS client class.
Public Functions
Creates an HTTP client.
- Parameters:
hostname – hostname
headerBuffer – header buffer
-
HttpClient(const std::string &hostname, size_t headerBufferSize = defaultHeaderBufferSize)
Creates an HTTP client.
- Parameters:
hostname – hostname
headerBufferSize – header buffer size
Creates an HTTPS client.
- Parameters:
hostname – hostname
serverCertificate – server certificate
headerBuffer – header buffer
-
HttpClient(const std::string &hostname, const char *serverCertificate, size_t headerBufferSize = defaultHeaderBufferSize)
Creates an HTTPS client.
- Parameters:
hostname – hostname
serverCertificate – server certificate
headerBufferSize – header buffer size
Creates an HTTPS client.
- Parameters:
hostname – hostname
crt_bundle_attach – function pointer to esp_crt_bundle_attach
headerBuffer – header buffer
-
HttpClient(const std::string &hostname, esp_err_t (*crt_bundle_attach)(void *conf), size_t headerBufferSize = defaultHeaderBufferSize)
Creates an HTTPS client.
- Parameters:
hostname – hostname
crt_bundle_attach – function pointer to esp_crt_bundle_attach
headerBufferSize – header buffer size
-
virtual esp_err_t Lock(TickType_t timeout = portMAX_DELAY) override
Locks the object.
- Parameters:
timeout – timeout in FreeRTOS ticks
- Returns:
error code
-
virtual esp_err_t Unlock() override
Unlocks the object.
- Returns:
error code
-
esp_err_t Initialize()
Initializes the client.
- Returns:
error code
-
esp_err_t WriteRequestHeaders(HttpMethod method, const std::string &uri, size_t bodySize)
Writes the request headers.
- Parameters:
method – HTTP method
uri – URI
bodySize – body size
- Returns:
error code
-
esp_err_t WriteRequestBody(const void *src, size_t size)
Writes the request body.
- Parameters:
src – source
size – number of bytes to write
- Returns:
error code
-
esp_err_t WriteRequest(HttpMethod method, const std::string &uri, const std::string &body)
Writes the request.
- Parameters:
method – HTTP method
uri – URI
body – body
- Returns:
error code
-
esp_err_t WriteRequest(HttpMethod method, const std::string &uri)
Writes the request with an empty body.
- Parameters:
method – HTTP method
uri – URI
- Returns:
-
esp_err_t ReadResponseHeaders(ushort &statusCode, size_t *bodySize)
Reads the response headers.
- Parameters:
statusCode – status code
bodySize – body size
- Returns:
error code
-
esp_err_t ReadResponseBody(void *dest, size_t size)
Reads the response body.
- Parameters:
dest – destination (can be NULL)
size – number of bytes to read
- Returns:
error code
-
esp_err_t Disconnect()
Disconnects from the server.
- Returns:
error code
-
uint16_t GetPort()
Gets the remote port.
- Returns:
port
-
esp_err_t SetPort(uint16_t port)
Sets the remote port.
- Parameters:
port – port
- Returns:
error code
-
TickType_t GetReadTimeout()
Gets the read operation timeout.
- Returns:
timeout in FreeRTOS ticks
-
esp_err_t SetReadTimeout(TickType_t timeout)
Sets the read operation timeout.
- Parameters:
timeout – timeout in FreeRTOS ticks
- Returns:
error code
-
esp_err_t SetAuthScheme(HttpAuthScheme scheme)
Sets the request authentication scheme.
- Parameters:
scheme – authentication scheme
- Returns:
error code
-
esp_err_t SetAuthCredentials(const std::string &username, const std::string &password)
Sets the request authentication credentials.
- Parameters:
username – username
password – password
- Returns:
error code
-
esp_err_t SetRequestHeader(const std::string &name, const std::string &value)
Sets the request header.
- Parameters:
name – header name
value – header value
- Returns:
error code
-
esp_err_t DeleteRequestHeader(const std::string &name)
Deletes the request header.
- Parameters:
name – header name
- Returns:
error code
-
const char *GetResponseHeader(const std::string &name)
Gets the response header value.
- Parameters:
name – header name
- Returns:
header value (NULL if no such header in the request)
Public Static Attributes
-
static const uint16_t defaultHttpPort = 80
Default HTTP port.
-
static const uint16_t defaultHttpsPort = 443
Default HTTPS port.
-
static const TickType_t defaultReadTimeout = 5000 / portTICK_PERIOD_MS
Default read operation timeout in FreeRTOS ticks.
-
static const size_t defaultHeaderBufferSize = 1024
Default header buffer size.