Chapter 2. API Reference
• esp_http_client_write(): Upload data, max length equal to write_len of
esp_http_client_open() function. We may not need to call it if write_len=0
• esp_http_client_fetch_headers(): After sending the headers and write data (if any) to the server,
this function will read the HTTP Server response headers. Calling this function will return the content-
length from the Server, and we can call esp_http_client_get_status_code() for the HTTP
status of the connection.
• esp_http_client_read(): Now, we can read the HTTP stream by this function.
• esp_http_client_close(): We should the connection after finish
• esp_http_client_cleanup(): And release the resources
Perform HTTP request as Stream reader Check the example function
http_perform_as_stream_reader at protocols/esp_http_client.
HTTP Authentication
The HTTP client supports both Basic and Digest Authentication. By providing usernames and passwords in url or in
the username, password of config entry. And with auth_type = HTTP_AUTH_TYPE_BASIC, the HTTP
client takes only 1 perform to pass the authentication process. If auth_type = HTTP_AUTH_TYPE_NONE,
but there are username and password in the configuration, the HTTP client takes 2 performs. The first time it
connects to the server and receives the UNAUTHORIZED header. Based on this information, it will know which
authentication method to choose, and perform it on the second.
Config authentication example with URI
esp_http_client_config_t config = {
.url = "http://user:passwd@httpbin.org/basic-auth/user/passwd",
.auth_type = HTTP_AUTH_TYPE_BASIC,
};
Config authentication example with username, password entry
esp_http_client_config_t config = {
.url = "http://httpbin.org/basic-auth/user/passwd",
.username = "user",
.password = "passwd",
.auth_type = HTTP_AUTH_TYPE_BASIC,
};
HTTP Client example: protocols/esp_http_client.
API Reference
Header File
• components/esp_http_client/include/esp_http_client.h
Functions
esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *config)
Start a HTTP session This function must be the first function to call, and it returns a esp_http_client_handle_t
that you must use as input to other functions in the interface. This call MUST have a corresponding call to
esp_http_client_cleanup when the operation is complete.
Return
• esp_http_client_handle_t
• NULL if any errors
Parameters
• [in] config: The configurations, see http_client_config_t
Espressif Systems 560
Submit Document Feedback
Release v4.4