mirror of
https://github.com/espressif/ESP8266_RTOS_SDK.git
synced 2025-06-01 16:16:54 +08:00
feat(http): Bring esp_http_client component and example from idf
Commit ID: dd3c0329
This commit is contained in:
67
examples/protocols/esp_http_client/esp_http_client_test.py
Normal file
67
examples/protocols/esp_http_client/esp_http_client_test.py
Normal file
@ -0,0 +1,67 @@
|
||||
import re
|
||||
import os
|
||||
|
||||
import ttfw_idf
|
||||
|
||||
|
||||
@ttfw_idf.idf_example_test(env_tag="Example_EthKitV1")
|
||||
def test_examples_protocol_esp_http_client(env, extra_data):
|
||||
"""
|
||||
steps: |
|
||||
1. join AP
|
||||
2. Send HTTP request to httpbin.org
|
||||
"""
|
||||
dut1 = env.get_dut("esp_http_client", "examples/protocols/esp_http_client", dut_class=ttfw_idf.ESP32DUT)
|
||||
# check and log bin size
|
||||
binary_file = os.path.join(dut1.app.binary_path, "esp-http-client-example.bin")
|
||||
bin_size = os.path.getsize(binary_file)
|
||||
ttfw_idf.log_performance("esp_http_client_bin_size", "{}KB".format(bin_size // 1024))
|
||||
ttfw_idf.check_performance("esp_http_client_bin_size", bin_size // 1024, dut1.TARGET)
|
||||
# start test
|
||||
dut1.start_app()
|
||||
dut1.expect("Connected to AP, begin http example", timeout=30)
|
||||
dut1.expect(re.compile(r"HTTP GET Status = 200, content_length = (\d)"))
|
||||
dut1.expect(re.compile(r"HTTP POST Status = 200, content_length = (\d)"))
|
||||
dut1.expect(re.compile(r"HTTP PUT Status = 200, content_length = (\d)"))
|
||||
dut1.expect(re.compile(r"HTTP PATCH Status = 200, content_length = (\d)"))
|
||||
dut1.expect(re.compile(r"HTTP DELETE Status = 200, content_length = (\d)"))
|
||||
dut1.expect(re.compile(r"HTTP HEAD Status = 200, content_length = (\d)"))
|
||||
dut1.expect(re.compile(r"HTTP Basic Auth Status = 200, content_length = (\d)"))
|
||||
dut1.expect(re.compile(r"HTTP Basic Auth redirect Status = 200, content_length = (\d)"))
|
||||
dut1.expect(re.compile(r"HTTP Digest Auth Status = 200, content_length = (\d)"))
|
||||
dut1.expect(re.compile(r"HTTPS Status = 200, content_length = (\d)"))
|
||||
dut1.expect(re.compile(r"HTTP chunk encoding Status = 200, content_length = (-?\d)"))
|
||||
# content-len for chunked encoding is typically -1, could be a positive length in some cases
|
||||
dut1.expect(re.compile(r"HTTP Stream reader Status = 200, content_length = (\d)"))
|
||||
dut1.expect(re.compile(r"Last esp error code: 0x8001"))
|
||||
dut1.expect("Finish http example")
|
||||
|
||||
# test mbedtls dynamic resource
|
||||
dut1 = env.get_dut("esp_http_client", "examples/protocols/esp_http_client", dut_class=ttfw_idf.ESP32DUT, app_config_name='ssldyn')
|
||||
# check and log bin size
|
||||
binary_file = os.path.join(dut1.app.binary_path, "esp-http-client-example.bin")
|
||||
bin_size = os.path.getsize(binary_file)
|
||||
ttfw_idf.log_performance("esp_http_client_bin_size", "{}KB".format(bin_size // 1024))
|
||||
ttfw_idf.check_performance("esp_http_client_bin_size", bin_size // 1024, dut1.TARGET)
|
||||
# start test
|
||||
dut1.start_app()
|
||||
dut1.expect("Connected to AP, begin http example", timeout=30)
|
||||
dut1.expect(re.compile(r"HTTP GET Status = 200, content_length = (\d)"))
|
||||
dut1.expect(re.compile(r"HTTP POST Status = 200, content_length = (\d)"))
|
||||
dut1.expect(re.compile(r"HTTP PUT Status = 200, content_length = (\d)"))
|
||||
dut1.expect(re.compile(r"HTTP PATCH Status = 200, content_length = (\d)"))
|
||||
dut1.expect(re.compile(r"HTTP DELETE Status = 200, content_length = (\d)"))
|
||||
dut1.expect(re.compile(r"HTTP HEAD Status = 200, content_length = (\d)"))
|
||||
dut1.expect(re.compile(r"HTTP Basic Auth Status = 200, content_length = (\d)"))
|
||||
dut1.expect(re.compile(r"HTTP Basic Auth redirect Status = 200, content_length = (\d)"))
|
||||
dut1.expect(re.compile(r"HTTP Digest Auth Status = 200, content_length = (\d)"))
|
||||
dut1.expect(re.compile(r"HTTPS Status = 200, content_length = (\d)"))
|
||||
dut1.expect(re.compile(r"HTTP chunk encoding Status = 200, content_length = (-?\d)"))
|
||||
# content-len for chunked encoding is typically -1, could be a positive length in some cases
|
||||
dut1.expect(re.compile(r"HTTP Stream reader Status = 200, content_length = (\d)"))
|
||||
dut1.expect(re.compile(r"Last esp error code: 0x8001"))
|
||||
dut1.expect("Finish http example")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_examples_protocol_esp_http_client()
|
@ -1,8 +1,6 @@
|
||||
set(COMPONENT_SRCS "esp_http_client_example.c")
|
||||
|
||||
# Embed the server root certificate into the final binary
|
||||
#
|
||||
# (If this was a component, we would set COMPONENT_EMBED_TXTFILES here.)
|
||||
set(COMPONENT_EMBED_TXTFILES howsmyssl_com_root_cert.pem)
|
||||
|
||||
register_component()
|
||||
idf_component_register(SRCS "esp_http_client_example.c"
|
||||
INCLUDE_DIRS "."
|
||||
EMBED_TXTFILES howsmyssl_com_root_cert.pem)
|
@ -9,20 +9,20 @@
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_netif.h"
|
||||
#include "esp_event.h"
|
||||
#include "protocol_examples_common.h"
|
||||
#include "nvs.h"
|
||||
#include "esp_system.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_netif.h"
|
||||
#include "protocol_examples_common.h"
|
||||
#include "esp_tls.h"
|
||||
|
||||
#include "esp_http_client.h"
|
||||
|
||||
#define MAX_HTTP_RECV_BUFFER 512
|
||||
#define MAX_HTTP_OUTPUT_BUFFER 2048
|
||||
static const char *TAG = "HTTP_CLIENT";
|
||||
|
||||
/* Root cert for howsmyssl.com, taken from howsmyssl_com_root_cert.pem
|
||||
@ -40,6 +40,8 @@ extern const char howsmyssl_com_root_cert_pem_end[] asm("_binary_howsmyssl_com
|
||||
|
||||
esp_err_t _http_event_handler(esp_http_client_event_t *evt)
|
||||
{
|
||||
static char *output_buffer; // Buffer to store response of http request from event handler
|
||||
static int output_len; // Stores number of bytes read
|
||||
switch(evt->event_id) {
|
||||
case HTTP_EVENT_ERROR:
|
||||
ESP_LOGD(TAG, "HTTP_EVENT_ERROR");
|
||||
@ -55,27 +57,73 @@ esp_err_t _http_event_handler(esp_http_client_event_t *evt)
|
||||
break;
|
||||
case HTTP_EVENT_ON_DATA:
|
||||
ESP_LOGD(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len);
|
||||
/*
|
||||
* Check for chunked encoding is added as the URL for chunked encoding used in this example returns binary data.
|
||||
* However, event handler can also be used in case chunked encoding is used.
|
||||
*/
|
||||
if (!esp_http_client_is_chunked_response(evt->client)) {
|
||||
// Write out data
|
||||
// printf("%.*s", evt->data_len, (char*)evt->data);
|
||||
// If user_data buffer is configured, copy the response into the buffer
|
||||
if (evt->user_data) {
|
||||
memcpy(evt->user_data + output_len, evt->data, evt->data_len);
|
||||
} else {
|
||||
if (output_buffer == NULL) {
|
||||
output_buffer = (char *) malloc(esp_http_client_get_content_length(evt->client));
|
||||
output_len = 0;
|
||||
if (output_buffer == NULL) {
|
||||
ESP_LOGE(TAG, "Failed to allocate memory for output buffer");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
}
|
||||
memcpy(output_buffer + output_len, evt->data, evt->data_len);
|
||||
}
|
||||
output_len += evt->data_len;
|
||||
}
|
||||
|
||||
break;
|
||||
case HTTP_EVENT_ON_FINISH:
|
||||
ESP_LOGD(TAG, "HTTP_EVENT_ON_FINISH");
|
||||
if (output_buffer != NULL) {
|
||||
// Response is accumulated in output_buffer. Uncomment the below line to print the accumulated response
|
||||
// ESP_LOG_BUFFER_HEX(TAG, output_buffer, output_len);
|
||||
free(output_buffer);
|
||||
output_buffer = NULL;
|
||||
output_len = 0;
|
||||
}
|
||||
break;
|
||||
case HTTP_EVENT_DISCONNECTED:
|
||||
ESP_LOGD(TAG, "HTTP_EVENT_DISCONNECTED");
|
||||
ESP_LOGI(TAG, "HTTP_EVENT_DISCONNECTED");
|
||||
int mbedtls_err = 0;
|
||||
esp_err_t err = esp_tls_get_and_clear_last_error(evt->data, &mbedtls_err, NULL);
|
||||
if (err != 0) {
|
||||
if (output_buffer != NULL) {
|
||||
free(output_buffer);
|
||||
output_buffer = NULL;
|
||||
output_len = 0;
|
||||
}
|
||||
ESP_LOGI(TAG, "Last esp error code: 0x%x", err);
|
||||
ESP_LOGI(TAG, "Last mbedtls failure: 0x%x", mbedtls_err);
|
||||
}
|
||||
break;
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static void http_rest()
|
||||
static void http_rest_with_url(void)
|
||||
{
|
||||
char local_response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0};
|
||||
/**
|
||||
* NOTE: All the configuration parameters for http_client must be spefied either in URL or as host and path parameters.
|
||||
* If host and path parameters are not set, query parameter will be ignored. In such cases,
|
||||
* query parameter should be specified in URL.
|
||||
*
|
||||
* If URL as well as host and path parameters are specified, values of host and path will be considered.
|
||||
*/
|
||||
esp_http_client_config_t config = {
|
||||
.url = "http://httpbin.org/get",
|
||||
.host = "httpbin.org",
|
||||
.path = "/get",
|
||||
.query = "esp",
|
||||
.event_handler = _http_event_handler,
|
||||
.user_data = local_response_buffer, // Pass address of local buffer to get response
|
||||
};
|
||||
esp_http_client_handle_t client = esp_http_client_init(&config);
|
||||
|
||||
@ -86,13 +134,15 @@ static void http_rest()
|
||||
esp_http_client_get_status_code(client),
|
||||
esp_http_client_get_content_length(client));
|
||||
} else {
|
||||
ESP_LOGE(TAG, "HTTP GET request failed: %d", err);
|
||||
ESP_LOGE(TAG, "HTTP GET request failed: %s", esp_err_to_name(err));
|
||||
}
|
||||
ESP_LOG_BUFFER_HEX(TAG, local_response_buffer, strlen(local_response_buffer));
|
||||
|
||||
// POST
|
||||
const char *post_data = "field1=value1&field2=value2";
|
||||
const char *post_data = "{\"field1\":\"value1\"}";
|
||||
esp_http_client_set_url(client, "http://httpbin.org/post");
|
||||
esp_http_client_set_method(client, HTTP_METHOD_POST);
|
||||
esp_http_client_set_header(client, "Content-Type", "application/json");
|
||||
esp_http_client_set_post_field(client, post_data, strlen(post_data));
|
||||
err = esp_http_client_perform(client);
|
||||
if (err == ESP_OK) {
|
||||
@ -100,7 +150,7 @@ static void http_rest()
|
||||
esp_http_client_get_status_code(client),
|
||||
esp_http_client_get_content_length(client));
|
||||
} else {
|
||||
ESP_LOGE(TAG, "HTTP POST request failed: %d", err);
|
||||
ESP_LOGE(TAG, "HTTP POST request failed: %s", esp_err_to_name(err));
|
||||
}
|
||||
|
||||
//PUT
|
||||
@ -112,7 +162,7 @@ static void http_rest()
|
||||
esp_http_client_get_status_code(client),
|
||||
esp_http_client_get_content_length(client));
|
||||
} else {
|
||||
ESP_LOGE(TAG, "HTTP PUT request failed: %d", err);
|
||||
ESP_LOGE(TAG, "HTTP PUT request failed: %s", esp_err_to_name(err));
|
||||
}
|
||||
|
||||
//PATCH
|
||||
@ -125,7 +175,7 @@ static void http_rest()
|
||||
esp_http_client_get_status_code(client),
|
||||
esp_http_client_get_content_length(client));
|
||||
} else {
|
||||
ESP_LOGE(TAG, "HTTP PATCH request failed: %d", err);
|
||||
ESP_LOGE(TAG, "HTTP PATCH request failed: %s", esp_err_to_name(err));
|
||||
}
|
||||
|
||||
//DELETE
|
||||
@ -137,7 +187,7 @@ static void http_rest()
|
||||
esp_http_client_get_status_code(client),
|
||||
esp_http_client_get_content_length(client));
|
||||
} else {
|
||||
ESP_LOGE(TAG, "HTTP DELETE request failed: %d", err);
|
||||
ESP_LOGE(TAG, "HTTP DELETE request failed: %s", esp_err_to_name(err));
|
||||
}
|
||||
|
||||
//HEAD
|
||||
@ -149,13 +199,100 @@ static void http_rest()
|
||||
esp_http_client_get_status_code(client),
|
||||
esp_http_client_get_content_length(client));
|
||||
} else {
|
||||
ESP_LOGE(TAG, "HTTP HEAD request failed: %d", err);
|
||||
ESP_LOGE(TAG, "HTTP HEAD request failed: %s", esp_err_to_name(err));
|
||||
}
|
||||
|
||||
esp_http_client_cleanup(client);
|
||||
}
|
||||
|
||||
static void http_auth_basic()
|
||||
static void http_rest_with_hostname_path(void)
|
||||
{
|
||||
esp_http_client_config_t config = {
|
||||
.host = "httpbin.org",
|
||||
.path = "/get",
|
||||
.transport_type = HTTP_TRANSPORT_OVER_TCP,
|
||||
.event_handler = _http_event_handler,
|
||||
};
|
||||
esp_http_client_handle_t client = esp_http_client_init(&config);
|
||||
|
||||
// GET
|
||||
esp_err_t err = esp_http_client_perform(client);
|
||||
if (err == ESP_OK) {
|
||||
ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %d",
|
||||
esp_http_client_get_status_code(client),
|
||||
esp_http_client_get_content_length(client));
|
||||
} else {
|
||||
ESP_LOGE(TAG, "HTTP GET request failed: %s", esp_err_to_name(err));
|
||||
}
|
||||
|
||||
// POST
|
||||
const char *post_data = "field1=value1&field2=value2";
|
||||
esp_http_client_set_url(client, "/post");
|
||||
esp_http_client_set_method(client, HTTP_METHOD_POST);
|
||||
esp_http_client_set_post_field(client, post_data, strlen(post_data));
|
||||
err = esp_http_client_perform(client);
|
||||
if (err == ESP_OK) {
|
||||
ESP_LOGI(TAG, "HTTP POST Status = %d, content_length = %d",
|
||||
esp_http_client_get_status_code(client),
|
||||
esp_http_client_get_content_length(client));
|
||||
} else {
|
||||
ESP_LOGE(TAG, "HTTP POST request failed: %s", esp_err_to_name(err));
|
||||
}
|
||||
|
||||
//PUT
|
||||
esp_http_client_set_url(client, "/put");
|
||||
esp_http_client_set_method(client, HTTP_METHOD_PUT);
|
||||
err = esp_http_client_perform(client);
|
||||
if (err == ESP_OK) {
|
||||
ESP_LOGI(TAG, "HTTP PUT Status = %d, content_length = %d",
|
||||
esp_http_client_get_status_code(client),
|
||||
esp_http_client_get_content_length(client));
|
||||
} else {
|
||||
ESP_LOGE(TAG, "HTTP PUT request failed: %s", esp_err_to_name(err));
|
||||
}
|
||||
|
||||
//PATCH
|
||||
esp_http_client_set_url(client, "/patch");
|
||||
esp_http_client_set_method(client, HTTP_METHOD_PATCH);
|
||||
esp_http_client_set_post_field(client, NULL, 0);
|
||||
err = esp_http_client_perform(client);
|
||||
if (err == ESP_OK) {
|
||||
ESP_LOGI(TAG, "HTTP PATCH Status = %d, content_length = %d",
|
||||
esp_http_client_get_status_code(client),
|
||||
esp_http_client_get_content_length(client));
|
||||
} else {
|
||||
ESP_LOGE(TAG, "HTTP PATCH request failed: %s", esp_err_to_name(err));
|
||||
}
|
||||
|
||||
//DELETE
|
||||
esp_http_client_set_url(client, "/delete");
|
||||
esp_http_client_set_method(client, HTTP_METHOD_DELETE);
|
||||
err = esp_http_client_perform(client);
|
||||
if (err == ESP_OK) {
|
||||
ESP_LOGI(TAG, "HTTP DELETE Status = %d, content_length = %d",
|
||||
esp_http_client_get_status_code(client),
|
||||
esp_http_client_get_content_length(client));
|
||||
} else {
|
||||
ESP_LOGE(TAG, "HTTP DELETE request failed: %s", esp_err_to_name(err));
|
||||
}
|
||||
|
||||
//HEAD
|
||||
esp_http_client_set_url(client, "/get");
|
||||
esp_http_client_set_method(client, HTTP_METHOD_HEAD);
|
||||
err = esp_http_client_perform(client);
|
||||
if (err == ESP_OK) {
|
||||
ESP_LOGI(TAG, "HTTP HEAD Status = %d, content_length = %d",
|
||||
esp_http_client_get_status_code(client),
|
||||
esp_http_client_get_content_length(client));
|
||||
} else {
|
||||
ESP_LOGE(TAG, "HTTP HEAD request failed: %s", esp_err_to_name(err));
|
||||
}
|
||||
|
||||
esp_http_client_cleanup(client);
|
||||
}
|
||||
|
||||
#if CONFIG_ESP_HTTP_CLIENT_ENABLE_BASIC_AUTH
|
||||
static void http_auth_basic(void)
|
||||
{
|
||||
esp_http_client_config_t config = {
|
||||
.url = "http://user:passwd@httpbin.org/basic-auth/user/passwd",
|
||||
@ -170,12 +307,12 @@ static void http_auth_basic()
|
||||
esp_http_client_get_status_code(client),
|
||||
esp_http_client_get_content_length(client));
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Error perform http request %d", err);
|
||||
ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err));
|
||||
}
|
||||
esp_http_client_cleanup(client);
|
||||
}
|
||||
|
||||
static void http_auth_basic_redirect()
|
||||
static void http_auth_basic_redirect(void)
|
||||
{
|
||||
esp_http_client_config_t config = {
|
||||
.url = "http://user:passwd@httpbin.org/basic-auth/user/passwd",
|
||||
@ -183,18 +320,19 @@ static void http_auth_basic_redirect()
|
||||
};
|
||||
esp_http_client_handle_t client = esp_http_client_init(&config);
|
||||
esp_err_t err = esp_http_client_perform(client);
|
||||
|
||||
if (err == ESP_OK) {
|
||||
ESP_LOGI(TAG, "HTTP Basic Auth redirect Status = %d, content_length = %d",
|
||||
esp_http_client_get_status_code(client),
|
||||
esp_http_client_get_content_length(client));
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Error perform http request %d", err);
|
||||
ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err));
|
||||
}
|
||||
|
||||
esp_http_client_cleanup(client);
|
||||
}
|
||||
#endif
|
||||
|
||||
static void http_auth_digest()
|
||||
static void http_auth_digest(void)
|
||||
{
|
||||
esp_http_client_config_t config = {
|
||||
.url = "http://user:passwd@httpbin.org/digest-auth/auth/user/passwd/MD5/never",
|
||||
@ -202,17 +340,18 @@ static void http_auth_digest()
|
||||
};
|
||||
esp_http_client_handle_t client = esp_http_client_init(&config);
|
||||
esp_err_t err = esp_http_client_perform(client);
|
||||
|
||||
if (err == ESP_OK) {
|
||||
ESP_LOGI(TAG, "HTTP Digest Auth Status = %d, content_length = %d",
|
||||
esp_http_client_get_status_code(client),
|
||||
esp_http_client_get_content_length(client));
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Error perform http request %d", err);
|
||||
ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err));
|
||||
}
|
||||
esp_http_client_cleanup(client);
|
||||
}
|
||||
|
||||
static void https()
|
||||
static void https_with_url(void)
|
||||
{
|
||||
esp_http_client_config_t config = {
|
||||
.url = "https://www.howsmyssl.com",
|
||||
@ -227,12 +366,34 @@ static void https()
|
||||
esp_http_client_get_status_code(client),
|
||||
esp_http_client_get_content_length(client));
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Error perform http request %d", err);
|
||||
ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err));
|
||||
}
|
||||
esp_http_client_cleanup(client);
|
||||
}
|
||||
|
||||
static void http_relative_redirect()
|
||||
static void https_with_hostname_path(void)
|
||||
{
|
||||
esp_http_client_config_t config = {
|
||||
.host = "www.howsmyssl.com",
|
||||
.path = "/",
|
||||
.transport_type = HTTP_TRANSPORT_OVER_SSL,
|
||||
.event_handler = _http_event_handler,
|
||||
.cert_pem = howsmyssl_com_root_cert_pem_start,
|
||||
};
|
||||
esp_http_client_handle_t client = esp_http_client_init(&config);
|
||||
esp_err_t err = esp_http_client_perform(client);
|
||||
|
||||
if (err == ESP_OK) {
|
||||
ESP_LOGI(TAG, "HTTPS Status = %d, content_length = %d",
|
||||
esp_http_client_get_status_code(client),
|
||||
esp_http_client_get_content_length(client));
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err));
|
||||
}
|
||||
esp_http_client_cleanup(client);
|
||||
}
|
||||
|
||||
static void http_relative_redirect(void)
|
||||
{
|
||||
esp_http_client_config_t config = {
|
||||
.url = "http://httpbin.org/relative-redirect/3",
|
||||
@ -246,12 +407,12 @@ static void http_relative_redirect()
|
||||
esp_http_client_get_status_code(client),
|
||||
esp_http_client_get_content_length(client));
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Error perform http request %d", err);
|
||||
ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err));
|
||||
}
|
||||
esp_http_client_cleanup(client);
|
||||
}
|
||||
|
||||
static void http_absolute_redirect()
|
||||
static void http_absolute_redirect(void)
|
||||
{
|
||||
esp_http_client_config_t config = {
|
||||
.url = "http://httpbin.org/absolute-redirect/3",
|
||||
@ -265,12 +426,12 @@ static void http_absolute_redirect()
|
||||
esp_http_client_get_status_code(client),
|
||||
esp_http_client_get_content_length(client));
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Error perform http request %d", err);
|
||||
ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err));
|
||||
}
|
||||
esp_http_client_cleanup(client);
|
||||
}
|
||||
|
||||
static void http_redirect_to_https()
|
||||
static void http_redirect_to_https(void)
|
||||
{
|
||||
esp_http_client_config_t config = {
|
||||
.url = "http://httpbin.org/redirect-to?url=https%3A%2F%2Fwww.howsmyssl.com",
|
||||
@ -284,13 +445,13 @@ static void http_redirect_to_https()
|
||||
esp_http_client_get_status_code(client),
|
||||
esp_http_client_get_content_length(client));
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Error perform http request %d", err);
|
||||
ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err));
|
||||
}
|
||||
esp_http_client_cleanup(client);
|
||||
}
|
||||
|
||||
|
||||
static void http_download_chunk()
|
||||
static void http_download_chunk(void)
|
||||
{
|
||||
esp_http_client_config_t config = {
|
||||
.url = "http://httpbin.org/stream-bytes/8912",
|
||||
@ -304,12 +465,12 @@ static void http_download_chunk()
|
||||
esp_http_client_get_status_code(client),
|
||||
esp_http_client_get_content_length(client));
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Error perform http request %d", err);
|
||||
ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err));
|
||||
}
|
||||
esp_http_client_cleanup(client);
|
||||
}
|
||||
|
||||
static void http_perform_as_stream_reader()
|
||||
static void http_perform_as_stream_reader(void)
|
||||
{
|
||||
char *buffer = malloc(MAX_HTTP_RECV_BUFFER + 1);
|
||||
if (buffer == NULL) {
|
||||
@ -318,12 +479,11 @@ static void http_perform_as_stream_reader()
|
||||
}
|
||||
esp_http_client_config_t config = {
|
||||
.url = "http://httpbin.org/get",
|
||||
.event_handler = _http_event_handler,
|
||||
};
|
||||
esp_http_client_handle_t client = esp_http_client_init(&config);
|
||||
esp_err_t err;
|
||||
if ((err = esp_http_client_open(client, 0)) != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to open HTTP connection: %d", err);
|
||||
ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
|
||||
free(buffer);
|
||||
return;
|
||||
}
|
||||
@ -345,7 +505,7 @@ static void http_perform_as_stream_reader()
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
static void https_async()
|
||||
static void https_async(void)
|
||||
{
|
||||
esp_http_client_config_t config = {
|
||||
.url = "https://postman-echo.com/post",
|
||||
@ -372,37 +532,135 @@ static void https_async()
|
||||
esp_http_client_get_status_code(client),
|
||||
esp_http_client_get_content_length(client));
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Error perform http request %d", err);
|
||||
ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err));
|
||||
}
|
||||
esp_http_client_cleanup(client);
|
||||
}
|
||||
|
||||
static void https_with_invalid_url(void)
|
||||
{
|
||||
esp_http_client_config_t config = {
|
||||
.url = "https://not.existent.url",
|
||||
.event_handler = _http_event_handler,
|
||||
};
|
||||
esp_http_client_handle_t client = esp_http_client_init(&config);
|
||||
esp_err_t err = esp_http_client_perform(client);
|
||||
|
||||
if (err == ESP_OK) {
|
||||
ESP_LOGI(TAG, "HTTPS Status = %d, content_length = %d",
|
||||
esp_http_client_get_status_code(client),
|
||||
esp_http_client_get_content_length(client));
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err));
|
||||
}
|
||||
esp_http_client_cleanup(client);
|
||||
}
|
||||
|
||||
/*
|
||||
* http_native_request() demonstrates use of low level APIs to connect to a server,
|
||||
* make a http request and read response. Event handler is not used in this case.
|
||||
* Note: This approach should only be used in case use of low level APIs is required.
|
||||
* The easiest way is to use esp_http_perform()
|
||||
*/
|
||||
static void http_native_request(void)
|
||||
{
|
||||
char output_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0}; // Buffer to store response of http request
|
||||
int content_length = 0;
|
||||
esp_http_client_config_t config = {
|
||||
.url = "http://httpbin.org/get",
|
||||
};
|
||||
esp_http_client_handle_t client = esp_http_client_init(&config);
|
||||
|
||||
// GET Request
|
||||
esp_http_client_set_method(client, HTTP_METHOD_GET);
|
||||
esp_err_t err = esp_http_client_open(client, 0);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
|
||||
} else {
|
||||
content_length = esp_http_client_fetch_headers(client);
|
||||
if (content_length < 0) {
|
||||
ESP_LOGE(TAG, "HTTP client fetch headers failed");
|
||||
} else {
|
||||
int data_read = esp_http_client_read_response(client, output_buffer, MAX_HTTP_OUTPUT_BUFFER);
|
||||
if (data_read >= 0) {
|
||||
ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %d",
|
||||
esp_http_client_get_status_code(client),
|
||||
esp_http_client_get_content_length(client));
|
||||
ESP_LOG_BUFFER_HEX(TAG, output_buffer, strlen(output_buffer));
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Failed to read response");
|
||||
}
|
||||
}
|
||||
}
|
||||
esp_http_client_close(client);
|
||||
|
||||
// POST Request
|
||||
const char *post_data = "{\"field1\":\"value1\"}";
|
||||
esp_http_client_set_url(client, "http://httpbin.org/post");
|
||||
esp_http_client_set_method(client, HTTP_METHOD_POST);
|
||||
esp_http_client_set_header(client, "Content-Type", "application/json");
|
||||
err = esp_http_client_open(client, strlen(post_data));
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
|
||||
} else {
|
||||
int wlen = esp_http_client_write(client, post_data, strlen(post_data));
|
||||
if (wlen < 0) {
|
||||
ESP_LOGE(TAG, "Write failed");
|
||||
}
|
||||
int data_read = esp_http_client_read_response(client, output_buffer, MAX_HTTP_OUTPUT_BUFFER);
|
||||
if (data_read >= 0) {
|
||||
ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %d",
|
||||
esp_http_client_get_status_code(client),
|
||||
esp_http_client_get_content_length(client));
|
||||
ESP_LOG_BUFFER_HEX(TAG, output_buffer, strlen(output_buffer));
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Failed to read response");
|
||||
}
|
||||
}
|
||||
esp_http_client_cleanup(client);
|
||||
}
|
||||
|
||||
static void http_test_task(void *pvParameters)
|
||||
{
|
||||
ESP_LOGI(TAG, "Connected to AP, begin http example");
|
||||
http_rest();
|
||||
http_rest_with_url();
|
||||
http_rest_with_hostname_path();
|
||||
#if CONFIG_ESP_HTTP_CLIENT_ENABLE_BASIC_AUTH
|
||||
http_auth_basic();
|
||||
http_auth_basic_redirect();
|
||||
#endif
|
||||
http_auth_digest();
|
||||
http_relative_redirect();
|
||||
http_absolute_redirect();
|
||||
https();
|
||||
https_with_url();
|
||||
https_with_hostname_path();
|
||||
http_redirect_to_https();
|
||||
http_download_chunk();
|
||||
http_perform_as_stream_reader();
|
||||
https_async();
|
||||
https_with_invalid_url();
|
||||
http_native_request();
|
||||
|
||||
ESP_LOGI(TAG, "Finish http example");
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
void app_main()
|
||||
void app_main(void)
|
||||
{
|
||||
ESP_ERROR_CHECK(nvs_flash_init());
|
||||
esp_err_t ret = nvs_flash_init();
|
||||
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||
ESP_ERROR_CHECK(nvs_flash_erase());
|
||||
ret = nvs_flash_init();
|
||||
}
|
||||
ESP_ERROR_CHECK(ret);
|
||||
ESP_ERROR_CHECK(esp_netif_init());
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
|
||||
|
||||
/* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
|
||||
* Read "Establishing Wi-Fi or Ethernet Connection" section in
|
||||
* examples/protocols/README.md for more information about this function.
|
||||
*/
|
||||
ESP_ERROR_CHECK(example_connect());
|
||||
ESP_LOGI(TAG, "Connected to AP, begin http example");
|
||||
|
||||
xTaskCreate(&http_test_task, "http_test_task", 8192, NULL, 5, NULL);
|
||||
}
|
||||
|
10
examples/protocols/esp_http_client/sdkconfig.ci
Normal file
10
examples/protocols/esp_http_client/sdkconfig.ci
Normal file
@ -0,0 +1,10 @@
|
||||
CONFIG_EXAMPLE_CONNECT_ETHERNET=y
|
||||
CONFIG_EXAMPLE_CONNECT_WIFI=n
|
||||
CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET=y
|
||||
CONFIG_EXAMPLE_ETH_PHY_IP101=y
|
||||
CONFIG_EXAMPLE_ETH_MDC_GPIO=23
|
||||
CONFIG_EXAMPLE_ETH_MDIO_GPIO=18
|
||||
CONFIG_EXAMPLE_ETH_PHY_RST_GPIO=5
|
||||
CONFIG_EXAMPLE_ETH_PHY_ADDR=1
|
||||
CONFIG_EXAMPLE_CONNECT_IPV6=y
|
||||
CONFIG_ESP_HTTP_CLIENT_ENABLE_BASIC_AUTH=y
|
13
examples/protocols/esp_http_client/sdkconfig.ci.ssldyn
Normal file
13
examples/protocols/esp_http_client/sdkconfig.ci.ssldyn
Normal file
@ -0,0 +1,13 @@
|
||||
CONFIG_EXAMPLE_CONNECT_ETHERNET=y
|
||||
CONFIG_EXAMPLE_CONNECT_WIFI=n
|
||||
CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET=y
|
||||
CONFIG_EXAMPLE_ETH_PHY_IP101=y
|
||||
CONFIG_EXAMPLE_ETH_MDC_GPIO=23
|
||||
CONFIG_EXAMPLE_ETH_MDIO_GPIO=18
|
||||
CONFIG_EXAMPLE_ETH_PHY_RST_GPIO=5
|
||||
CONFIG_EXAMPLE_ETH_PHY_ADDR=1
|
||||
CONFIG_EXAMPLE_CONNECT_IPV6=y
|
||||
CONFIG_ESP_HTTP_CLIENT_ENABLE_BASIC_AUTH=y
|
||||
CONFIG_MBEDTLS_DYNAMIC_BUFFER=y
|
||||
CONFIG_MBEDTLS_DYNAMIC_FREE_PEER_CERT=y
|
||||
CONFIG_MBEDTLS_DYNAMIC_FREE_CONFIG_DATA=y
|
@ -1,3 +0,0 @@
|
||||
CONFIG_NEWLIB_LIBRARY_LEVEL_NORMAL=y
|
||||
CONFIG_MBEDTLS_SSL_OUT_CONTENT_LEN=1024
|
||||
CONFIG_MBEDTLS_SSL_IN_CONTENT_LEN=16384
|
Reference in New Issue
Block a user