idfxx 1.0.0
Modern C++23 components for ESP-IDF
Loading...
Searching...
No Matches
idfxx::http::client Class Reference

HTTP client with blocking and streaming request support. More...

Classes

struct  config
 HTTP client configuration. More...
 
class  error_category
 Error category for HTTP client errors. More...
 

Public Types

enum class  errc : esp_err_t {
  max_redirect = 0x7001 ,
  connect = 0x7002 ,
  write_data = 0x7003 ,
  fetch_header = 0x7004 ,
  invalid_transport = 0x7005 ,
  connecting = 0x7006 ,
  eagain = 0x7007 ,
  connection_closed = 0x7008
}
 Error codes for HTTP client operations. More...
 

Public Member Functions

 client (config cfg)
 Creates an HTTP client from configuration.
 
 ~client ()
 Destroys the client and releases all resources.
 
 client (const client &)=delete
 
clientoperator= (const client &)=delete
 
 client (client &&) noexcept
 Move constructor.
 
clientoperator= (client &&) noexcept
 Move assignment.
 
void set_url (std::string_view url)
 Sets the request URL.
 
result< voidtry_set_url (std::string_view url)
 Sets the request URL.
 
void set_method (enum method m)
 Sets the HTTP method.
 
void set_header (std::string_view key, std::string_view value)
 Sets a request header.
 
void delete_header (std::string_view key)
 Deletes a request header.
 
void set_post_field (std::string_view data)
 Sets the POST request body.
 
void set_username (std::string_view username)
 Sets the username for authentication.
 
void set_password (std::string_view password)
 Sets the password for authentication.
 
void set_auth_type (enum auth_type type)
 Sets the authentication type.
 
template<typename Rep , typename Period >
void set_timeout (const std::chrono::duration< Rep, Period > &timeout)
 Sets the request timeout.
 
void perform ()
 Performs a blocking HTTP request.
 
result< voidtry_perform ()
 Performs a blocking HTTP request.
 
void open (size_t write_len=0)
 Opens a connection for streaming.
 
void open (int write_len)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
size_t write (std::span< const uint8_t > data)
 Writes data to the open connection.
 
size_t write (std::string_view data)
 Writes string data to the open connection.
 
int64_t fetch_headers ()
 Reads and processes response headers.
 
size_t read (std::span< uint8_t > buf)
 Reads response body data.
 
size_t read (std::span< char > buf)
 Reads response body data into a character buffer.
 
void flush_response ()
 Discards any remaining response data.
 
void close ()
 Closes the connection.
 
result< voidtry_open (size_t write_len=0)
 Opens a connection for streaming.
 
result< voidtry_open (int write_len)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
result< size_ttry_write (std::span< const uint8_t > data)
 Writes data to the open connection.
 
result< size_ttry_write (std::string_view data)
 Writes string data to the open connection.
 
result< int64_ttry_fetch_headers ()
 Reads and processes response headers.
 
result< size_ttry_read (std::span< uint8_t > buf)
 Reads response body data.
 
result< size_ttry_read (std::span< char > buf)
 Reads response body data into a character buffer.
 
result< voidtry_flush_response ()
 Discards any remaining response data.
 
result< voidtry_close ()
 Closes the connection.
 
int status_code () const
 Returns the HTTP response status code.
 
int64_t content_length () const
 Returns the response content length.
 
bool is_chunked_response () const
 Checks whether the response uses chunked transfer encoding.
 
std::optional< std::string > get_header (std::string_view key) const
 Retrieves a response header value.
 
std::string get_url () const
 Retrieves the current effective URL.
 
void set_redirection ()
 Applies the current redirect URL.
 
result< voidtry_set_redirection ()
 Applies the current redirect URL.
 
void reset_redirect_counter ()
 Resets the redirect counter to zero.
 
esp_http_client_handle_t idf_handle () const noexcept
 Returns the underlying ESP-IDF HTTP client handle.
 

Static Public Member Functions

static result< clientmake (config cfg)
 Creates an HTTP client from configuration.
 

Detailed Description

HTTP client with blocking and streaming request support.

Manages an HTTP session with support for TLS, authentication, custom headers, redirects, and event callbacks. Provides both a simple blocking perform() API and a streaming open()/write()/read()/close() API.

Not thread-safe — callers must manage their own synchronization.

Definition at line 77 of file client.hpp.

Member Enumeration Documentation

◆ errc

Error codes for HTTP client operations.

Enumerator
max_redirect 

Maximum number of redirects exceeded.

connect 

Connection to server failed.

write_data 

Error writing request data.

fetch_header 

Error reading response headers.

invalid_transport 

Invalid transport type specified.

connecting 

Connection in progress (async mode)

eagain 

Resource temporarily unavailable, try again.

connection_closed 

Connection closed by remote.

Definition at line 82 of file client.hpp.

Constructor & Destructor Documentation

◆ client() [1/3]

idfxx::http::client::client ( config  cfg)
explicit

Creates an HTTP client from configuration.

Parameters
cfgClient configuration.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled in menuconfig.
Exceptions
std::system_erroron failure.

◆ ~client()

idfxx::http::client::~client ( )

Destroys the client and releases all resources.

◆ client() [2/3]

idfxx::http::client::client ( const client )
delete

◆ client() [3/3]

idfxx::http::client::client ( client &&  )
noexcept

Move constructor.

Transfers handle ownership.

Member Function Documentation

◆ close()

void idfxx::http::client::close ( )
inline

Closes the connection.

Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled in menuconfig.
Exceptions
std::system_erroron failure.

Definition at line 415 of file client.hpp.

References try_close(), and idfxx::unwrap().

◆ content_length()

int64_t idfxx::http::client::content_length ( ) const

Returns the response content length.

Returns
Content length in bytes, or -1 if unknown/chunked.

◆ delete_header()

void idfxx::http::client::delete_header ( std::string_view  key)

Deletes a request header.

Parameters
keyHeader name to remove.

◆ fetch_headers()

int64_t idfxx::http::client::fetch_headers ( )
inline

Reads and processes response headers.

Must be called after open() (and any write() calls) before reading response body data.

Returns
The content length, or -1 if chunked/unknown.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled in menuconfig.
Exceptions
std::system_erroron failure.

Definition at line 379 of file client.hpp.

References try_fetch_headers(), and idfxx::unwrap().

◆ flush_response()

void idfxx::http::client::flush_response ( )
inline

Discards any remaining response data.

Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled in menuconfig.
Exceptions
std::system_erroron failure.

Definition at line 407 of file client.hpp.

References try_flush_response(), and idfxx::unwrap().

◆ get_header()

std::optional< std::string > idfxx::http::client::get_header ( std::string_view  key) const

Retrieves a response header value.

Parameters
keyHeader name to look up.
Returns
The header value, or std::nullopt if the header is not present.

◆ get_url()

std::string idfxx::http::client::get_url ( ) const

Retrieves the current effective URL.

May differ from the originally configured URL after redirects.

Returns
The current URL.

◆ idf_handle()

esp_http_client_handle_t idfxx::http::client::idf_handle ( ) const
inlinenoexcept

Returns the underlying ESP-IDF HTTP client handle.

Returns
The esp_http_client handle.

Definition at line 571 of file client.hpp.

◆ is_chunked_response()

bool idfxx::http::client::is_chunked_response ( ) const

Checks whether the response uses chunked transfer encoding.

Returns
true if the response is chunked.

◆ make()

static result< client > idfxx::http::client::make ( config  cfg)
static

Creates an HTTP client from configuration.

Parameters
cfgClient configuration.
Returns
The new client, or an error.

◆ open() [1/2]

void idfxx::http::client::open ( int  write_len)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 347 of file client.hpp.

References open().

Referenced by open().

◆ open() [2/2]

void idfxx::http::client::open ( size_t  write_len = 0)
inline

Opens a connection for streaming.

Opens the HTTP connection and sends request headers. If write_len is greater than zero, the connection is opened for writing that many bytes before reading the response.

Parameters
write_lenNumber of bytes to write (0 for read-only requests).
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled in menuconfig.
Exceptions
std::system_errorif write_len exceeds INT_MAX or the connection fails.

Definition at line 344 of file client.hpp.

References try_open(), and idfxx::unwrap().

◆ operator=() [1/2]

client & idfxx::http::client::operator= ( client &&  )
noexcept

Move assignment.

Transfers handle ownership.

◆ operator=() [2/2]

client & idfxx::http::client::operator= ( const client )
delete

◆ perform()

void idfxx::http::client::perform ( )
inline

Performs a blocking HTTP request.

Sends the request and reads the complete response. Use event callbacks or the streaming API to process response data.

idfxx::http::client c({.url = "https://example.com"});
auto status = c.status_code();
HTTP client with blocking and streaming request support.
Definition client.hpp:77
void perform()
Performs a blocking HTTP request.
Definition client.hpp:315
std::expected< T, std::error_code > result
result type wrapping a value or error code.
Definition error.hpp:120
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled in menuconfig.
Exceptions
std::system_erroron failure.

Definition at line 315 of file client.hpp.

References try_perform(), and idfxx::unwrap().

◆ read() [1/2]

size_t idfxx::http::client::read ( std::span< char buf)
inline

Reads response body data into a character buffer.

Parameters
bufBuffer to read into.
Returns
Number of bytes read, or 0 at end of response.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled in menuconfig.
Exceptions
std::system_erroron failure.

Definition at line 399 of file client.hpp.

References try_read(), and idfxx::unwrap().

◆ read() [2/2]

size_t idfxx::http::client::read ( std::span< uint8_t buf)
inline

Reads response body data.

Parameters
bufBuffer to read into.
Returns
Number of bytes read, or 0 at end of response.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled in menuconfig.
Exceptions
std::system_erroron failure.

Definition at line 389 of file client.hpp.

References try_read(), and idfxx::unwrap().

◆ reset_redirect_counter()

void idfxx::http::client::reset_redirect_counter ( )

Resets the redirect counter to zero.

Allows additional redirects up to max_redirection_count.

◆ set_auth_type()

void idfxx::http::client::set_auth_type ( enum auth_type  type)

Sets the authentication type.

Parameters
typeThe authentication type.

◆ set_header()

void idfxx::http::client::set_header ( std::string_view  key,
std::string_view  value 
)

Sets a request header.

Throws std::bad_alloc (or aborts) on allocation failure.

Parameters
keyHeader name.
valueHeader value.

◆ set_method()

void idfxx::http::client::set_method ( enum method  m)

Sets the HTTP method.

Parameters
mThe HTTP method.

◆ set_password()

void idfxx::http::client::set_password ( std::string_view  password)

Sets the password for authentication.

Parameters
passwordThe password.

◆ set_post_field()

void idfxx::http::client::set_post_field ( std::string_view  data)

Sets the POST request body.

The client takes ownership of a copy of the data, keeping it alive for the duration of the request. Throws std::bad_alloc (or aborts) on allocation failure.

Parameters
dataPOST body content.

◆ set_redirection()

void idfxx::http::client::set_redirection ( )
inline

Applies the current redirect URL.

Sets the client URL to the redirect target from the Location header. Typically called from a redirect event callback when automatic redirect is disabled.

Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled in menuconfig.
Exceptions
std::system_erroron failure.

Definition at line 546 of file client.hpp.

References try_set_redirection(), and idfxx::unwrap().

◆ set_timeout()

template<typename Rep , typename Period >
void idfxx::http::client::set_timeout ( const std::chrono::duration< Rep, Period > &  timeout)
inline

Sets the request timeout.

Template Parameters
RepDuration representation type.
PeriodDuration period type.
Parameters
timeoutThe timeout duration.

Definition at line 291 of file client.hpp.

References idfxx::timeout.

◆ set_url()

void idfxx::http::client::set_url ( std::string_view  url)
inline

Sets the request URL.

Parameters
urlThe URL string.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled in menuconfig.
Exceptions
std::system_erroron failure.

Definition at line 215 of file client.hpp.

References try_set_url(), and idfxx::unwrap().

◆ set_username()

void idfxx::http::client::set_username ( std::string_view  username)

Sets the username for authentication.

Parameters
usernameThe username.

◆ status_code()

int idfxx::http::client::status_code ( ) const

Returns the HTTP response status code.

Returns
The status code (e.g. 200, 404).

◆ try_close()

result< void > idfxx::http::client::try_close ( )

Closes the connection.

Returns
Success, or an error.

Referenced by close().

◆ try_fetch_headers()

result< int64_t > idfxx::http::client::try_fetch_headers ( )

Reads and processes response headers.

Must be called after open() (and any write() calls) before reading response body data.

Returns
The content length (-1 if chunked/unknown), or an error.

Referenced by fetch_headers().

◆ try_flush_response()

result< void > idfxx::http::client::try_flush_response ( )

Discards any remaining response data.

Returns
Success, or an error.

Referenced by flush_response().

◆ try_open() [1/2]

result< void > idfxx::http::client::try_open ( int  write_len)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 432 of file client.hpp.

References try_open().

Referenced by try_open().

◆ try_open() [2/2]

result< void > idfxx::http::client::try_open ( size_t  write_len = 0)

Opens a connection for streaming.

Opens the HTTP connection and sends request headers. If write_len is greater than zero, the connection is opened for writing that many bytes before reading the response.

Parameters
write_lenNumber of bytes to write (0 for read-only requests).
Returns
Success, or an error.
Return values
idfxx::errc::invalid_argif write_len exceeds INT_MAX.

Referenced by open().

◆ try_perform()

result< void > idfxx::http::client::try_perform ( )

Performs a blocking HTTP request.

Sends the request and reads the complete response. Use event callbacks or the streaming API to process response data.

Returns
Success, or an error.

Referenced by perform().

◆ try_read() [1/2]

result< size_t > idfxx::http::client::try_read ( std::span< char buf)

Reads response body data into a character buffer.

Parameters
bufBuffer to read into.
Returns
Number of bytes read (0 at end of response), or an error.

◆ try_read() [2/2]

result< size_t > idfxx::http::client::try_read ( std::span< uint8_t buf)

Reads response body data.

Parameters
bufBuffer to read into.
Returns
Number of bytes read (0 at end of response), or an error.

Referenced by read(), and read().

◆ try_set_redirection()

result< void > idfxx::http::client::try_set_redirection ( )

Applies the current redirect URL.

Sets the client URL to the redirect target from the Location header. Typically called from a redirect event callback when automatic redirect is disabled.

Returns
Success, or an error.

Referenced by set_redirection().

◆ try_set_url()

result< void > idfxx::http::client::try_set_url ( std::string_view  url)

Sets the request URL.

Parameters
urlThe URL string.
Returns
Success, or an error.

Referenced by set_url().

◆ try_write() [1/2]

result< size_t > idfxx::http::client::try_write ( std::span< const uint8_t data)

Writes data to the open connection.

Parameters
dataBinary data to write.
Returns
Number of bytes written, or an error.

Referenced by write(), and write().

◆ try_write() [2/2]

result< size_t > idfxx::http::client::try_write ( std::string_view  data)

Writes string data to the open connection.

Parameters
dataString data to write.
Returns
Number of bytes written, or an error.

◆ write() [1/2]

size_t idfxx::http::client::write ( std::span< const uint8_t data)
inline

Writes data to the open connection.

Parameters
dataBinary data to write.
Returns
Number of bytes written.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled in menuconfig.
Exceptions
std::system_erroron failure.

Definition at line 357 of file client.hpp.

References try_write(), and idfxx::unwrap().

◆ write() [2/2]

size_t idfxx::http::client::write ( std::string_view  data)
inline

Writes string data to the open connection.

Parameters
dataString data to write.
Returns
Number of bytes written.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled in menuconfig.
Exceptions
std::system_erroron failure.

Definition at line 367 of file client.hpp.

References try_write(), and idfxx::unwrap().


The documentation for this class was generated from the following file: