22#include <idfxx/http/types>
26#include <esp_idf_version.h>
32#include <system_error>
35struct esp_http_client_event;
50#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
72 std::span<const uint8_t>
data;
122 std::string url = {};
123 std::string host = {};
125 std::string path = {};
126 std::string query = {};
132 std::string username = {};
133 std::string password = {};
137 std::string cert_pem = {};
138 std::string client_cert_pem = {};
139 std::string client_key_pem = {};
142 std::string user_agent = {};
145 bool disable_auto_redirect =
false;
146 int max_redirection_count = 0;
147 int max_authorization_retries = 0;
151 int buffer_size_tx = 0;
154 bool keep_alive_enable =
false;
155 std::chrono::seconds keep_alive_idle{5};
156 std::chrono::seconds keep_alive_interval{5};
157 int keep_alive_count = 3;
160 bool use_global_ca_store =
false;
161 bool skip_cert_common_name_check =
false;
162 std::string common_name = {};
174 std::move_only_function<
int(
void*)
const> crt_bundle_attach = {};
180#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
217#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
252 void set_header(std::string_view key, std::string_view value);
300 template<
typename Rep,
typename Period>
302 _set_timeout(std::chrono::ceil<std::chrono::milliseconds>(
timeout));
309#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
342#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
518#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
575#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
611#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
634#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
676 std::move_only_function<
void(
const event_data&)
const> on_event,
677 std::move_only_function<
int(
void*)
const> crt_bundle_attach,
679 std::string cert_pem,
680 std::string client_cert_pem,
681 std::string client_key_pem
684 void _set_timeout(std::chrono::milliseconds
timeout);
688 const std::string& cert_pem,
689 const std::string& client_cert_pem,
690 const std::string& client_key_pem
697 std::move_only_function<
int(
void*)
const> _crt_bundle_attach;
698 std::string _post_data;
699 std::string _cert_pem;
700 std::string _client_cert_pem;
701 std::string _client_key_pem;
746#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
762struct is_error_code_enum<
idfxx::http::client::errc> : true_type {};
766#include "sdkconfig.h"
767#ifdef CONFIG_IDFXX_STD_FORMAT
774struct formatter<
idfxx::http::event_id> {
775 constexpr auto parse(format_parse_context& ctx) {
return ctx.begin(); }
777 template<
typename FormatContext>
780 return std::copy(s.begin(), s.end(), ctx.out());
784#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
786struct formatter<
idfxx::http::client::state> {
787 constexpr auto parse(format_parse_context& ctx) {
return ctx.begin(); }
789 template<
typename FormatContext>
792 return std::copy(str.begin(), str.end(), ctx.out());
Error category for HTTP client errors.
std::string message(int ec) const override final
Returns a human-readable message for the given error code.
const char * name() const noexcept override final
Returns the name of the error category.
HTTP client with blocking and streaming request support.
void delete_header(std::string_view key)
Deletes a request header.
result< void > try_set_url(std::string_view url)
Sets the request URL.
client(client &&) noexcept
Move constructor.
result< size_t > try_read(std::span< char > buf)
Reads response body data into a character buffer.
result< int > try_chunk_length() const
Returns the length of the current chunk in a chunked response.
void perform()
Performs a blocking HTTP request.
void set_url(std::string_view url)
Sets the request URL.
result< size_t > try_write(std::span< const uint8_t > data)
Writes data to the open connection.
void open(size_t write_len=0)
Opens a connection for streaming.
void cancel_request()
Cancels an ongoing HTTP request.
int64_t content_length() const
Returns the response content length.
client(config cfg)
Creates an HTTP client from configuration.
void set_password(std::string_view password)
Sets the password for authentication.
result< void > try_cancel_request()
Cancels an ongoing HTTP request.
void set_auth_type(enum auth_type type)
Sets the authentication type.
void set_username(std::string_view username)
Sets the username for authentication.
size_t write(std::span< const uint8_t > data)
Writes data to the open connection.
void close()
Closes the connection.
void reset_redirect_counter()
Resets the redirect counter to zero.
result< void > try_perform()
Performs a blocking HTTP request.
result< void > try_set_redirection()
Applies the current redirect URL.
result< void > try_close()
Closes the connection.
size_t read(std::span< char > buf)
Reads response body data into a character buffer.
result< size_t > try_write(std::string_view data)
Writes string data to the open connection.
std::optional< std::string > get_header(std::string_view key) const
Retrieves a response header value.
result< int64_t > try_fetch_headers()
Reads and processes response headers.
enum state get_state() const
Returns the current lifecycle state of the client.
static result< client > make(config cfg)
Creates an HTTP client from configuration.
result< void > try_flush_response()
Discards any remaining response data.
void set_method(enum method m)
Sets the HTTP method.
void open(int write_len)
This is an overloaded member function, provided for convenience. It differs from the above function o...
void set_header(std::string_view key, std::string_view value)
Sets a request header.
client & operator=(const client &)=delete
void set_redirection()
Applies the current redirect URL.
client(const client &)=delete
~client()
Destroys the client and releases all resources.
void set_timeout(const std::chrono::duration< Rep, Period > &timeout)
Sets the request timeout.
bool is_chunked_response() const
Checks whether the response uses chunked transfer encoding.
int status_code() const
Returns the HTTP response status code.
void flush_response()
Discards any remaining response data.
void set_post_field(std::string_view data)
Sets the POST request body.
result< void > try_open(int write_len)
This is an overloaded member function, provided for convenience. It differs from the above function o...
state
HTTP client lifecycle state.
@ req_complete_data
Request data sent.
@ connected
Connected to server.
@ uninit
Client not yet initialized.
@ req_complete_header
Request headers sent.
@ res_complete_data
Response data completed.
@ res_on_data_start
Response data started.
@ res_complete_header
Response headers received.
@ init
Client initialized.
result< size_t > try_read(std::span< uint8_t > buf)
Reads response body data.
int64_t fetch_headers()
Reads and processes response headers.
errc
Error codes for HTTP client operations.
@ max_redirect
Maximum number of redirects exceeded.
@ fetch_header
Error reading response headers.
@ connection_closed
Connection closed by remote.
@ write_data
Error writing request data.
@ connecting
Connection in progress (async mode)
@ eagain
Resource temporarily unavailable, try again.
@ connect
Connection to server failed.
@ invalid_transport
Invalid transport type specified.
int chunk_length() const
Returns the length of the current chunk in a chunked response.
result< void > try_open(size_t write_len=0)
Opens a connection for streaming.
size_t read(std::span< uint8_t > buf)
Reads response body data.
std::string get_url() const
Retrieves the current effective URL.
size_t write(std::string_view data)
Writes string data to the open connection.
esp_http_client_handle_t idf_handle() const noexcept
Returns the underlying ESP-IDF HTTP client handle.
Concept for types that can be both received and posted as event data.
std::string to_string(core_id c)
Returns a string representation of a CPU core identifier.
struct esp_http_client * esp_http_client_handle_t
method
HTTP request methods.
auth_type
HTTP authentication types.
event_id
HTTP client event identifiers.
@ on_data
Received response body data.
@ on_header
Received a response header.
@ on_headers_complete
All response headers have been received.
@ disconnected
Disconnected from the server.
@ on_status_code
Received the HTTP status code from the server.
@ on_connected
Connected to the server.
@ headers_sent
All request headers have been sent.
@ on_finish
Finished a complete HTTP session.
@ error
An error occurred during the HTTP operation.
@ redirect
Intercepting a redirect.
std::unexpected< std::error_code > http_client_error(esp_err_t e)
Creates an unexpected error from an ESP-IDF error code, mapping to HTTP client error codes where poss...
std::error_code make_error_code(client::errc e) noexcept
Creates an error code from an idfxx::http::client::errc value.
const client::error_category & http_client_category() noexcept
Returns a reference to the HTTP client error category singleton.
transport
HTTP transport types.
@ unknown
Unknown transport.
T unwrap(result< T > result)
Throws a std::system_error if the result is an error.
@ timeout
Operation timed out.
std::expected< T, std::error_code > result
result type wrapping a value or error code.
HTTP client configuration.
std::string_view header_key
Header name for on_header events.
std::string_view header_value
Header value for on_header events.
std::span< const uint8_t > data
Payload for on_data events.
enum event_id id
Event type.