Chapter 4. Software framework
4.5.2 HTTP
Does ESP8266 support HTTP hosting?
Yes, it does. ESP8266 can run as a server in both SoftAP and Station modes.
• When running as a server in SoftAP mode, clients can directly access the ESP8266 host or server
at 192.168.4.1 (default) IP address.
• When the server is accessed via a router, the IP address should be the one allocated to the ESP8266
by the router.
• When using SDK to write native code, please refer to relevant examples.
• When using AT commands, start a server using AT+CIPSERVER command.
How to use esp_http_client to send chunked data?
• Please use HTTP Stream by setting the write_len parameter of esp_http_client_open()
to -1. Then the “Transfer-Encoding”will be set to “chunked”automatically,please see
http_client_prepare_first_line() in esp_http_client.c.
• The code snippet is listed below for your reference:
static void http_post_chunked_data()
{
esp_http_client_config_t config = {
.url = "http://httpbin.org/post",
.method = HTTP_METHOD_POST, // This is NOT required. write_len < 0 will␣
,→force POST anyway
};
char buffer[MAX_HTTP_OUTPUT_BUFFER] = {0};
esp_http_client_handle_t client = esp_http_client_init(&config);
esp_err_t err = esp_http_client_open(client, -1); // write_len=-1 sets␣
,→header "Transfer-Encoding: chunked" and method to POST
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
return;
}
// Post some data
esp_http_client_write(client, "5", 1); // length
esp_http_client_write(client, "\r\n", 2);
esp_http_client_write(client, "Hello", 5); // data
esp_http_client_write(client, "\r\n", 2);
esp_http_client_write(client, "7", 1); // length
esp_http_client_write(client, "\r\n", 2);
esp_http_client_write(client, " World!", 7); // data
esp_http_client_write(client, "\r\n", 2);
esp_http_client_write(client, "0", 1); // end
esp_http_client_write(client, "\r\n", 2);
esp_http_client_write(client, "\r\n", 2);
// After the POST is complete, you can examine the response as required␣
,→using:
int content_length = esp_http_client_fetch_headers(client);
(continues on next page)
Espressif Systems 91
Submit Document Feedback
Release master