| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735 |
- /* ESP HTTP Client Example
- This example code is in the Public Domain (or CC0 licensed, at your option.)
- Unless required by applicable law or agreed to in writing, this
- software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
- CONDITIONS OF ANY KIND, either express or implied.
- */
- #include <string.h>
- #include <stdlib.h>
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #include "esp_log.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_crt_bundle.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
- The PEM file was extracted from the output of this command:
- openssl s_client -showcerts -connect www.howsmyssl.com:443 </dev/null
- The CA root cert is the last cert given in the chain of certs.
- To embed it in the app binary, the PEM file is named
- in the component.mk COMPONENT_EMBED_TXTFILES variable.
- */
- extern const char howsmyssl_com_root_cert_pem_start[] asm("_binary_howsmyssl_com_root_cert_pem_start");
- extern const char howsmyssl_com_root_cert_pem_end[] asm("_binary_howsmyssl_com_root_cert_pem_end");
- extern const char postman_root_cert_pem_start[] asm("_binary_postman_root_cert_pem_start");
- extern const char postman_root_cert_pem_end[] asm("_binary_postman_root_cert_pem_end");
- 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");
- break;
- case HTTP_EVENT_ON_CONNECTED:
- ESP_LOGD(TAG, "HTTP_EVENT_ON_CONNECTED");
- break;
- case HTTP_EVENT_HEADER_SENT:
- ESP_LOGD(TAG, "HTTP_EVENT_HEADER_SENT");
- break;
- case HTTP_EVENT_ON_HEADER:
- ESP_LOGD(TAG, "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value);
- 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)) {
- // 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_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_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 = {
- .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
- .disable_auto_redirect = true,
- };
- 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));
- }
- ESP_LOG_BUFFER_HEX(TAG, local_response_buffer, strlen(local_response_buffer));
- // POST
- 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) {
- 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, "http://httpbin.org/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, "http://httpbin.org/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, "http://httpbin.org/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, "http://httpbin.org/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);
- }
- 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)
- {
- /**
- * Note: `max_authorization_retries` in esp_http_client_config_t
- * can be used to configure number of retry attempts to be performed
- * in case unauthorized status code is received.
- *
- * To disable authorization retries, set max_authorization_retries to -1.
- */
- esp_http_client_config_t config = {
- .url = "http://user:passwd@httpbin.org/basic-auth/user/passwd",
- .event_handler = _http_event_handler,
- .auth_type = HTTP_AUTH_TYPE_BASIC,
- .max_authorization_retries = -1,
- };
- 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 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_auth_basic_redirect(void)
- {
- esp_http_client_config_t config = {
- .url = "http://user:passwd@httpbin.org/basic-auth/user/passwd",
- .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, "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 %s", esp_err_to_name(err));
- }
- esp_http_client_cleanup(client);
- }
- #endif
- #if CONFIG_ESP_HTTP_CLIENT_ENABLE_DIGEST_AUTH
- 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",
- .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, "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 %s", esp_err_to_name(err));
- }
- esp_http_client_cleanup(client);
- }
- #endif
- static void https_with_url(void)
- {
- esp_http_client_config_t config = {
- .url = "https://www.howsmyssl.com",
- .event_handler = _http_event_handler,
- .crt_bundle_attach = esp_crt_bundle_attach,
- };
- 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 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",
- .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, "HTTP Relative path 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 %s", esp_err_to_name(err));
- }
- esp_http_client_cleanup(client);
- }
- static void http_absolute_redirect(void)
- {
- esp_http_client_config_t config = {
- .url = "http://httpbin.org/absolute-redirect/3",
- .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, "HTTP Absolute path 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 %s", esp_err_to_name(err));
- }
- esp_http_client_cleanup(client);
- }
- 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",
- .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, "HTTP redirect to 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_download_chunk(void)
- {
- esp_http_client_config_t config = {
- .url = "http://httpbin.org/stream-bytes/8912",
- .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, "HTTP chunk encoding 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_perform_as_stream_reader(void)
- {
- char *buffer = malloc(MAX_HTTP_RECV_BUFFER + 1);
- if (buffer == NULL) {
- ESP_LOGE(TAG, "Cannot malloc http receive buffer");
- return;
- }
- esp_http_client_config_t config = {
- .url = "http://httpbin.org/get",
- };
- 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: %s", esp_err_to_name(err));
- free(buffer);
- return;
- }
- int content_length = esp_http_client_fetch_headers(client);
- int total_read_len = 0, read_len;
- if (total_read_len < content_length && content_length <= MAX_HTTP_RECV_BUFFER) {
- read_len = esp_http_client_read(client, buffer, content_length);
- if (read_len <= 0) {
- ESP_LOGE(TAG, "Error read data");
- }
- buffer[read_len] = 0;
- ESP_LOGD(TAG, "read_len = %d", read_len);
- }
- ESP_LOGI(TAG, "HTTP Stream reader Status = %d, content_length = %d",
- esp_http_client_get_status_code(client),
- esp_http_client_get_content_length(client));
- esp_http_client_close(client);
- esp_http_client_cleanup(client);
- free(buffer);
- }
- static void https_async(void)
- {
- esp_http_client_config_t config = {
- .url = "https://postman-echo.com/post",
- .event_handler = _http_event_handler,
- .cert_pem = postman_root_cert_pem_start,
- .is_async = true,
- .timeout_ms = 5000,
- };
- esp_http_client_handle_t client = esp_http_client_init(&config);
- esp_err_t err;
- const char *post_data = "Using a Palantír requires a person with great strength of will and wisdom. The Palantíri were meant to "
- "be used by the Dúnedain to communicate throughout the Realms in Exile. During the War of the Ring, "
- "the Palantíri were used by many individuals. Sauron used the Ithil-stone to take advantage of the users "
- "of the other two stones, the Orthanc-stone and Anor-stone, but was also susceptible to deception himself.";
- esp_http_client_set_method(client, HTTP_METHOD_POST);
- esp_http_client_set_post_field(client, post_data, strlen(post_data));
- while (1) {
- err = esp_http_client_perform(client);
- if (err != ESP_ERR_HTTP_EAGAIN) {
- break;
- }
- }
- 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 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, data_read);
- } 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");
- }
- 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 POST 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_partial_download(void)
- {
- esp_http_client_config_t config = {
- .url = "http://jigsaw.w3.org/HTTP/TE/foo.txt",
- .event_handler = _http_event_handler,
- };
- esp_http_client_handle_t client = esp_http_client_init(&config);
- // Download a file excluding first 10 bytes
- esp_http_client_set_header(client, "Range", "bytes=10-");
- esp_err_t err = esp_http_client_perform(client);
- if (err == ESP_OK) {
- ESP_LOGI(TAG, "HTTP Status = %d, content_length = %d",
- esp_http_client_get_status_code(client),
- esp_http_client_get_content_length(client));
- } else {
- ESP_LOGE(TAG, "HTTP request failed: %s", esp_err_to_name(err));
- }
- // Download last 10 bytes of a file
- esp_http_client_set_header(client, "Range", "bytes=-10");
- err = esp_http_client_perform(client);
- if (err == ESP_OK) {
- ESP_LOGI(TAG, "HTTP Status = %d, content_length = %d",
- esp_http_client_get_status_code(client),
- esp_http_client_get_content_length(client));
- } else {
- ESP_LOGE(TAG, "HTTP request failed: %s", esp_err_to_name(err));
- }
- // Download 10 bytes from 11 to 20
- esp_http_client_set_header(client, "Range", "bytes=11-20");
- err = esp_http_client_perform(client);
- if (err == ESP_OK) {
- ESP_LOGI(TAG, "HTTP Status = %d, content_length = %d",
- esp_http_client_get_status_code(client),
- esp_http_client_get_content_length(client));
- } else {
- ESP_LOGE(TAG, "HTTP request failed: %s", esp_err_to_name(err));
- }
- esp_http_client_cleanup(client);
- }
- static void http_test_task(void *pvParameters)
- {
- 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
- #if CONFIG_ESP_HTTP_CLIENT_ENABLE_DIGEST_AUTH
- http_auth_digest();
- #endif
- http_relative_redirect();
- http_absolute_redirect();
- 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();
- http_partial_download();
- ESP_LOGI(TAG, "Finish http example");
- vTaskDelete(NULL);
- }
- void app_main(void)
- {
- 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);
- }
|