esp_http_client_example.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. /* ESP HTTP Client Example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include "freertos/FreeRTOS.h"
  10. #include "freertos/task.h"
  11. #include "esp_log.h"
  12. #include "esp_system.h"
  13. #include "nvs_flash.h"
  14. #include "esp_event.h"
  15. #include "esp_netif.h"
  16. #include "protocol_examples_common.h"
  17. #include "esp_tls.h"
  18. #include "esp_crt_bundle.h"
  19. #include "esp_http_client.h"
  20. #define MAX_HTTP_RECV_BUFFER 512
  21. #define MAX_HTTP_OUTPUT_BUFFER 2048
  22. static const char *TAG = "HTTP_CLIENT";
  23. /* Root cert for howsmyssl.com, taken from howsmyssl_com_root_cert.pem
  24. The PEM file was extracted from the output of this command:
  25. openssl s_client -showcerts -connect www.howsmyssl.com:443 </dev/null
  26. The CA root cert is the last cert given in the chain of certs.
  27. To embed it in the app binary, the PEM file is named
  28. in the component.mk COMPONENT_EMBED_TXTFILES variable.
  29. */
  30. extern const char howsmyssl_com_root_cert_pem_start[] asm("_binary_howsmyssl_com_root_cert_pem_start");
  31. extern const char howsmyssl_com_root_cert_pem_end[] asm("_binary_howsmyssl_com_root_cert_pem_end");
  32. extern const char postman_root_cert_pem_start[] asm("_binary_postman_root_cert_pem_start");
  33. extern const char postman_root_cert_pem_end[] asm("_binary_postman_root_cert_pem_end");
  34. esp_err_t _http_event_handler(esp_http_client_event_t *evt)
  35. {
  36. static char *output_buffer; // Buffer to store response of http request from event handler
  37. static int output_len; // Stores number of bytes read
  38. switch(evt->event_id) {
  39. case HTTP_EVENT_ERROR:
  40. ESP_LOGD(TAG, "HTTP_EVENT_ERROR");
  41. break;
  42. case HTTP_EVENT_ON_CONNECTED:
  43. ESP_LOGD(TAG, "HTTP_EVENT_ON_CONNECTED");
  44. break;
  45. case HTTP_EVENT_HEADER_SENT:
  46. ESP_LOGD(TAG, "HTTP_EVENT_HEADER_SENT");
  47. break;
  48. case HTTP_EVENT_ON_HEADER:
  49. ESP_LOGD(TAG, "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value);
  50. break;
  51. case HTTP_EVENT_ON_DATA:
  52. ESP_LOGD(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len);
  53. /*
  54. * Check for chunked encoding is added as the URL for chunked encoding used in this example returns binary data.
  55. * However, event handler can also be used in case chunked encoding is used.
  56. */
  57. if (!esp_http_client_is_chunked_response(evt->client)) {
  58. // If user_data buffer is configured, copy the response into the buffer
  59. if (evt->user_data) {
  60. memcpy(evt->user_data + output_len, evt->data, evt->data_len);
  61. } else {
  62. if (output_buffer == NULL) {
  63. output_buffer = (char *) malloc(esp_http_client_get_content_length(evt->client));
  64. output_len = 0;
  65. if (output_buffer == NULL) {
  66. ESP_LOGE(TAG, "Failed to allocate memory for output buffer");
  67. return ESP_FAIL;
  68. }
  69. }
  70. memcpy(output_buffer + output_len, evt->data, evt->data_len);
  71. }
  72. output_len += evt->data_len;
  73. }
  74. break;
  75. case HTTP_EVENT_ON_FINISH:
  76. ESP_LOGD(TAG, "HTTP_EVENT_ON_FINISH");
  77. if (output_buffer != NULL) {
  78. // Response is accumulated in output_buffer. Uncomment the below line to print the accumulated response
  79. // ESP_LOG_BUFFER_HEX(TAG, output_buffer, output_len);
  80. free(output_buffer);
  81. output_buffer = NULL;
  82. }
  83. output_len = 0;
  84. break;
  85. case HTTP_EVENT_DISCONNECTED:
  86. ESP_LOGI(TAG, "HTTP_EVENT_DISCONNECTED");
  87. int mbedtls_err = 0;
  88. esp_err_t err = esp_tls_get_and_clear_last_error(evt->data, &mbedtls_err, NULL);
  89. if (err != 0) {
  90. ESP_LOGI(TAG, "Last esp error code: 0x%x", err);
  91. ESP_LOGI(TAG, "Last mbedtls failure: 0x%x", mbedtls_err);
  92. }
  93. if (output_buffer != NULL) {
  94. free(output_buffer);
  95. output_buffer = NULL;
  96. }
  97. output_len = 0;
  98. break;
  99. }
  100. return ESP_OK;
  101. }
  102. static void http_rest_with_url(void)
  103. {
  104. char local_response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0};
  105. /**
  106. * NOTE: All the configuration parameters for http_client must be spefied either in URL or as host and path parameters.
  107. * If host and path parameters are not set, query parameter will be ignored. In such cases,
  108. * query parameter should be specified in URL.
  109. *
  110. * If URL as well as host and path parameters are specified, values of host and path will be considered.
  111. */
  112. esp_http_client_config_t config = {
  113. .host = "httpbin.org",
  114. .path = "/get",
  115. .query = "esp",
  116. .event_handler = _http_event_handler,
  117. .user_data = local_response_buffer, // Pass address of local buffer to get response
  118. .disable_auto_redirect = true,
  119. };
  120. esp_http_client_handle_t client = esp_http_client_init(&config);
  121. // GET
  122. esp_err_t err = esp_http_client_perform(client);
  123. if (err == ESP_OK) {
  124. ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %d",
  125. esp_http_client_get_status_code(client),
  126. esp_http_client_get_content_length(client));
  127. } else {
  128. ESP_LOGE(TAG, "HTTP GET request failed: %s", esp_err_to_name(err));
  129. }
  130. ESP_LOG_BUFFER_HEX(TAG, local_response_buffer, strlen(local_response_buffer));
  131. // POST
  132. const char *post_data = "{\"field1\":\"value1\"}";
  133. esp_http_client_set_url(client, "http://httpbin.org/post");
  134. esp_http_client_set_method(client, HTTP_METHOD_POST);
  135. esp_http_client_set_header(client, "Content-Type", "application/json");
  136. esp_http_client_set_post_field(client, post_data, strlen(post_data));
  137. err = esp_http_client_perform(client);
  138. if (err == ESP_OK) {
  139. ESP_LOGI(TAG, "HTTP POST Status = %d, content_length = %d",
  140. esp_http_client_get_status_code(client),
  141. esp_http_client_get_content_length(client));
  142. } else {
  143. ESP_LOGE(TAG, "HTTP POST request failed: %s", esp_err_to_name(err));
  144. }
  145. //PUT
  146. esp_http_client_set_url(client, "http://httpbin.org/put");
  147. esp_http_client_set_method(client, HTTP_METHOD_PUT);
  148. err = esp_http_client_perform(client);
  149. if (err == ESP_OK) {
  150. ESP_LOGI(TAG, "HTTP PUT Status = %d, content_length = %d",
  151. esp_http_client_get_status_code(client),
  152. esp_http_client_get_content_length(client));
  153. } else {
  154. ESP_LOGE(TAG, "HTTP PUT request failed: %s", esp_err_to_name(err));
  155. }
  156. //PATCH
  157. esp_http_client_set_url(client, "http://httpbin.org/patch");
  158. esp_http_client_set_method(client, HTTP_METHOD_PATCH);
  159. esp_http_client_set_post_field(client, NULL, 0);
  160. err = esp_http_client_perform(client);
  161. if (err == ESP_OK) {
  162. ESP_LOGI(TAG, "HTTP PATCH Status = %d, content_length = %d",
  163. esp_http_client_get_status_code(client),
  164. esp_http_client_get_content_length(client));
  165. } else {
  166. ESP_LOGE(TAG, "HTTP PATCH request failed: %s", esp_err_to_name(err));
  167. }
  168. //DELETE
  169. esp_http_client_set_url(client, "http://httpbin.org/delete");
  170. esp_http_client_set_method(client, HTTP_METHOD_DELETE);
  171. err = esp_http_client_perform(client);
  172. if (err == ESP_OK) {
  173. ESP_LOGI(TAG, "HTTP DELETE Status = %d, content_length = %d",
  174. esp_http_client_get_status_code(client),
  175. esp_http_client_get_content_length(client));
  176. } else {
  177. ESP_LOGE(TAG, "HTTP DELETE request failed: %s", esp_err_to_name(err));
  178. }
  179. //HEAD
  180. esp_http_client_set_url(client, "http://httpbin.org/get");
  181. esp_http_client_set_method(client, HTTP_METHOD_HEAD);
  182. err = esp_http_client_perform(client);
  183. if (err == ESP_OK) {
  184. ESP_LOGI(TAG, "HTTP HEAD Status = %d, content_length = %d",
  185. esp_http_client_get_status_code(client),
  186. esp_http_client_get_content_length(client));
  187. } else {
  188. ESP_LOGE(TAG, "HTTP HEAD request failed: %s", esp_err_to_name(err));
  189. }
  190. esp_http_client_cleanup(client);
  191. }
  192. static void http_rest_with_hostname_path(void)
  193. {
  194. esp_http_client_config_t config = {
  195. .host = "httpbin.org",
  196. .path = "/get",
  197. .transport_type = HTTP_TRANSPORT_OVER_TCP,
  198. .event_handler = _http_event_handler,
  199. };
  200. esp_http_client_handle_t client = esp_http_client_init(&config);
  201. // GET
  202. esp_err_t err = esp_http_client_perform(client);
  203. if (err == ESP_OK) {
  204. ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %d",
  205. esp_http_client_get_status_code(client),
  206. esp_http_client_get_content_length(client));
  207. } else {
  208. ESP_LOGE(TAG, "HTTP GET request failed: %s", esp_err_to_name(err));
  209. }
  210. // POST
  211. const char *post_data = "field1=value1&field2=value2";
  212. esp_http_client_set_url(client, "/post");
  213. esp_http_client_set_method(client, HTTP_METHOD_POST);
  214. esp_http_client_set_post_field(client, post_data, strlen(post_data));
  215. err = esp_http_client_perform(client);
  216. if (err == ESP_OK) {
  217. ESP_LOGI(TAG, "HTTP POST Status = %d, content_length = %d",
  218. esp_http_client_get_status_code(client),
  219. esp_http_client_get_content_length(client));
  220. } else {
  221. ESP_LOGE(TAG, "HTTP POST request failed: %s", esp_err_to_name(err));
  222. }
  223. //PUT
  224. esp_http_client_set_url(client, "/put");
  225. esp_http_client_set_method(client, HTTP_METHOD_PUT);
  226. err = esp_http_client_perform(client);
  227. if (err == ESP_OK) {
  228. ESP_LOGI(TAG, "HTTP PUT Status = %d, content_length = %d",
  229. esp_http_client_get_status_code(client),
  230. esp_http_client_get_content_length(client));
  231. } else {
  232. ESP_LOGE(TAG, "HTTP PUT request failed: %s", esp_err_to_name(err));
  233. }
  234. //PATCH
  235. esp_http_client_set_url(client, "/patch");
  236. esp_http_client_set_method(client, HTTP_METHOD_PATCH);
  237. esp_http_client_set_post_field(client, NULL, 0);
  238. err = esp_http_client_perform(client);
  239. if (err == ESP_OK) {
  240. ESP_LOGI(TAG, "HTTP PATCH Status = %d, content_length = %d",
  241. esp_http_client_get_status_code(client),
  242. esp_http_client_get_content_length(client));
  243. } else {
  244. ESP_LOGE(TAG, "HTTP PATCH request failed: %s", esp_err_to_name(err));
  245. }
  246. //DELETE
  247. esp_http_client_set_url(client, "/delete");
  248. esp_http_client_set_method(client, HTTP_METHOD_DELETE);
  249. err = esp_http_client_perform(client);
  250. if (err == ESP_OK) {
  251. ESP_LOGI(TAG, "HTTP DELETE Status = %d, content_length = %d",
  252. esp_http_client_get_status_code(client),
  253. esp_http_client_get_content_length(client));
  254. } else {
  255. ESP_LOGE(TAG, "HTTP DELETE request failed: %s", esp_err_to_name(err));
  256. }
  257. //HEAD
  258. esp_http_client_set_url(client, "/get");
  259. esp_http_client_set_method(client, HTTP_METHOD_HEAD);
  260. err = esp_http_client_perform(client);
  261. if (err == ESP_OK) {
  262. ESP_LOGI(TAG, "HTTP HEAD Status = %d, content_length = %d",
  263. esp_http_client_get_status_code(client),
  264. esp_http_client_get_content_length(client));
  265. } else {
  266. ESP_LOGE(TAG, "HTTP HEAD request failed: %s", esp_err_to_name(err));
  267. }
  268. esp_http_client_cleanup(client);
  269. }
  270. #if CONFIG_ESP_HTTP_CLIENT_ENABLE_BASIC_AUTH
  271. static void http_auth_basic(void)
  272. {
  273. /**
  274. * Note: `max_authorization_retries` in esp_http_client_config_t
  275. * can be used to configure number of retry attempts to be performed
  276. * in case unauthorized status code is received.
  277. *
  278. * To disable authorization retries, set max_authorization_retries to -1.
  279. */
  280. esp_http_client_config_t config = {
  281. .url = "http://user:passwd@httpbin.org/basic-auth/user/passwd",
  282. .event_handler = _http_event_handler,
  283. .auth_type = HTTP_AUTH_TYPE_BASIC,
  284. .max_authorization_retries = -1,
  285. };
  286. esp_http_client_handle_t client = esp_http_client_init(&config);
  287. esp_err_t err = esp_http_client_perform(client);
  288. if (err == ESP_OK) {
  289. ESP_LOGI(TAG, "HTTP Basic Auth Status = %d, content_length = %d",
  290. esp_http_client_get_status_code(client),
  291. esp_http_client_get_content_length(client));
  292. } else {
  293. ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err));
  294. }
  295. esp_http_client_cleanup(client);
  296. }
  297. static void http_auth_basic_redirect(void)
  298. {
  299. esp_http_client_config_t config = {
  300. .url = "http://user:passwd@httpbin.org/basic-auth/user/passwd",
  301. .event_handler = _http_event_handler,
  302. };
  303. esp_http_client_handle_t client = esp_http_client_init(&config);
  304. esp_err_t err = esp_http_client_perform(client);
  305. if (err == ESP_OK) {
  306. ESP_LOGI(TAG, "HTTP Basic Auth redirect Status = %d, content_length = %d",
  307. esp_http_client_get_status_code(client),
  308. esp_http_client_get_content_length(client));
  309. } else {
  310. ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err));
  311. }
  312. esp_http_client_cleanup(client);
  313. }
  314. #endif
  315. #if CONFIG_ESP_HTTP_CLIENT_ENABLE_DIGEST_AUTH
  316. static void http_auth_digest(void)
  317. {
  318. esp_http_client_config_t config = {
  319. .url = "http://user:passwd@httpbin.org/digest-auth/auth/user/passwd/MD5/never",
  320. .event_handler = _http_event_handler,
  321. };
  322. esp_http_client_handle_t client = esp_http_client_init(&config);
  323. esp_err_t err = esp_http_client_perform(client);
  324. if (err == ESP_OK) {
  325. ESP_LOGI(TAG, "HTTP Digest Auth Status = %d, content_length = %d",
  326. esp_http_client_get_status_code(client),
  327. esp_http_client_get_content_length(client));
  328. } else {
  329. ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err));
  330. }
  331. esp_http_client_cleanup(client);
  332. }
  333. #endif
  334. static void https_with_url(void)
  335. {
  336. esp_http_client_config_t config = {
  337. .url = "https://www.howsmyssl.com",
  338. .event_handler = _http_event_handler,
  339. .crt_bundle_attach = esp_crt_bundle_attach,
  340. };
  341. esp_http_client_handle_t client = esp_http_client_init(&config);
  342. esp_err_t err = esp_http_client_perform(client);
  343. if (err == ESP_OK) {
  344. ESP_LOGI(TAG, "HTTPS Status = %d, content_length = %d",
  345. esp_http_client_get_status_code(client),
  346. esp_http_client_get_content_length(client));
  347. } else {
  348. ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err));
  349. }
  350. esp_http_client_cleanup(client);
  351. }
  352. static void https_with_hostname_path(void)
  353. {
  354. esp_http_client_config_t config = {
  355. .host = "www.howsmyssl.com",
  356. .path = "/",
  357. .transport_type = HTTP_TRANSPORT_OVER_SSL,
  358. .event_handler = _http_event_handler,
  359. .cert_pem = howsmyssl_com_root_cert_pem_start,
  360. };
  361. esp_http_client_handle_t client = esp_http_client_init(&config);
  362. esp_err_t err = esp_http_client_perform(client);
  363. if (err == ESP_OK) {
  364. ESP_LOGI(TAG, "HTTPS Status = %d, content_length = %d",
  365. esp_http_client_get_status_code(client),
  366. esp_http_client_get_content_length(client));
  367. } else {
  368. ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err));
  369. }
  370. esp_http_client_cleanup(client);
  371. }
  372. static void http_relative_redirect(void)
  373. {
  374. esp_http_client_config_t config = {
  375. .url = "http://httpbin.org/relative-redirect/3",
  376. .event_handler = _http_event_handler,
  377. };
  378. esp_http_client_handle_t client = esp_http_client_init(&config);
  379. esp_err_t err = esp_http_client_perform(client);
  380. if (err == ESP_OK) {
  381. ESP_LOGI(TAG, "HTTP Relative path redirect Status = %d, content_length = %d",
  382. esp_http_client_get_status_code(client),
  383. esp_http_client_get_content_length(client));
  384. } else {
  385. ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err));
  386. }
  387. esp_http_client_cleanup(client);
  388. }
  389. static void http_absolute_redirect(void)
  390. {
  391. esp_http_client_config_t config = {
  392. .url = "http://httpbin.org/absolute-redirect/3",
  393. .event_handler = _http_event_handler,
  394. };
  395. esp_http_client_handle_t client = esp_http_client_init(&config);
  396. esp_err_t err = esp_http_client_perform(client);
  397. if (err == ESP_OK) {
  398. ESP_LOGI(TAG, "HTTP Absolute path redirect Status = %d, content_length = %d",
  399. esp_http_client_get_status_code(client),
  400. esp_http_client_get_content_length(client));
  401. } else {
  402. ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err));
  403. }
  404. esp_http_client_cleanup(client);
  405. }
  406. static void http_redirect_to_https(void)
  407. {
  408. esp_http_client_config_t config = {
  409. .url = "http://httpbin.org/redirect-to?url=https%3A%2F%2Fwww.howsmyssl.com",
  410. .event_handler = _http_event_handler,
  411. };
  412. esp_http_client_handle_t client = esp_http_client_init(&config);
  413. esp_err_t err = esp_http_client_perform(client);
  414. if (err == ESP_OK) {
  415. ESP_LOGI(TAG, "HTTP redirect to HTTPS Status = %d, content_length = %d",
  416. esp_http_client_get_status_code(client),
  417. esp_http_client_get_content_length(client));
  418. } else {
  419. ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err));
  420. }
  421. esp_http_client_cleanup(client);
  422. }
  423. static void http_download_chunk(void)
  424. {
  425. esp_http_client_config_t config = {
  426. .url = "http://httpbin.org/stream-bytes/8912",
  427. .event_handler = _http_event_handler,
  428. };
  429. esp_http_client_handle_t client = esp_http_client_init(&config);
  430. esp_err_t err = esp_http_client_perform(client);
  431. if (err == ESP_OK) {
  432. ESP_LOGI(TAG, "HTTP chunk encoding Status = %d, content_length = %d",
  433. esp_http_client_get_status_code(client),
  434. esp_http_client_get_content_length(client));
  435. } else {
  436. ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err));
  437. }
  438. esp_http_client_cleanup(client);
  439. }
  440. static void http_perform_as_stream_reader(void)
  441. {
  442. char *buffer = malloc(MAX_HTTP_RECV_BUFFER + 1);
  443. if (buffer == NULL) {
  444. ESP_LOGE(TAG, "Cannot malloc http receive buffer");
  445. return;
  446. }
  447. esp_http_client_config_t config = {
  448. .url = "http://httpbin.org/get",
  449. };
  450. esp_http_client_handle_t client = esp_http_client_init(&config);
  451. esp_err_t err;
  452. if ((err = esp_http_client_open(client, 0)) != ESP_OK) {
  453. ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
  454. free(buffer);
  455. return;
  456. }
  457. int content_length = esp_http_client_fetch_headers(client);
  458. int total_read_len = 0, read_len;
  459. if (total_read_len < content_length && content_length <= MAX_HTTP_RECV_BUFFER) {
  460. read_len = esp_http_client_read(client, buffer, content_length);
  461. if (read_len <= 0) {
  462. ESP_LOGE(TAG, "Error read data");
  463. }
  464. buffer[read_len] = 0;
  465. ESP_LOGD(TAG, "read_len = %d", read_len);
  466. }
  467. ESP_LOGI(TAG, "HTTP Stream reader Status = %d, content_length = %d",
  468. esp_http_client_get_status_code(client),
  469. esp_http_client_get_content_length(client));
  470. esp_http_client_close(client);
  471. esp_http_client_cleanup(client);
  472. free(buffer);
  473. }
  474. static void https_async(void)
  475. {
  476. esp_http_client_config_t config = {
  477. .url = "https://postman-echo.com/post",
  478. .event_handler = _http_event_handler,
  479. .cert_pem = postman_root_cert_pem_start,
  480. .is_async = true,
  481. .timeout_ms = 5000,
  482. };
  483. esp_http_client_handle_t client = esp_http_client_init(&config);
  484. esp_err_t err;
  485. const char *post_data = "Using a Palantír requires a person with great strength of will and wisdom. The Palantíri were meant to "
  486. "be used by the Dúnedain to communicate throughout the Realms in Exile. During the War of the Ring, "
  487. "the Palantíri were used by many individuals. Sauron used the Ithil-stone to take advantage of the users "
  488. "of the other two stones, the Orthanc-stone and Anor-stone, but was also susceptible to deception himself.";
  489. esp_http_client_set_method(client, HTTP_METHOD_POST);
  490. esp_http_client_set_post_field(client, post_data, strlen(post_data));
  491. while (1) {
  492. err = esp_http_client_perform(client);
  493. if (err != ESP_ERR_HTTP_EAGAIN) {
  494. break;
  495. }
  496. }
  497. if (err == ESP_OK) {
  498. ESP_LOGI(TAG, "HTTPS Status = %d, content_length = %d",
  499. esp_http_client_get_status_code(client),
  500. esp_http_client_get_content_length(client));
  501. } else {
  502. ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err));
  503. }
  504. esp_http_client_cleanup(client);
  505. }
  506. static void https_with_invalid_url(void)
  507. {
  508. esp_http_client_config_t config = {
  509. .url = "https://not.existent.url",
  510. .event_handler = _http_event_handler,
  511. };
  512. esp_http_client_handle_t client = esp_http_client_init(&config);
  513. esp_err_t err = esp_http_client_perform(client);
  514. if (err == ESP_OK) {
  515. ESP_LOGI(TAG, "HTTPS Status = %d, content_length = %d",
  516. esp_http_client_get_status_code(client),
  517. esp_http_client_get_content_length(client));
  518. } else {
  519. ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err));
  520. }
  521. esp_http_client_cleanup(client);
  522. }
  523. /*
  524. * http_native_request() demonstrates use of low level APIs to connect to a server,
  525. * make a http request and read response. Event handler is not used in this case.
  526. * Note: This approach should only be used in case use of low level APIs is required.
  527. * The easiest way is to use esp_http_perform()
  528. */
  529. static void http_native_request(void)
  530. {
  531. char output_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0}; // Buffer to store response of http request
  532. int content_length = 0;
  533. esp_http_client_config_t config = {
  534. .url = "http://httpbin.org/get",
  535. };
  536. esp_http_client_handle_t client = esp_http_client_init(&config);
  537. // GET Request
  538. esp_http_client_set_method(client, HTTP_METHOD_GET);
  539. esp_err_t err = esp_http_client_open(client, 0);
  540. if (err != ESP_OK) {
  541. ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
  542. } else {
  543. content_length = esp_http_client_fetch_headers(client);
  544. if (content_length < 0) {
  545. ESP_LOGE(TAG, "HTTP client fetch headers failed");
  546. } else {
  547. int data_read = esp_http_client_read_response(client, output_buffer, MAX_HTTP_OUTPUT_BUFFER);
  548. if (data_read >= 0) {
  549. ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %d",
  550. esp_http_client_get_status_code(client),
  551. esp_http_client_get_content_length(client));
  552. ESP_LOG_BUFFER_HEX(TAG, output_buffer, data_read);
  553. } else {
  554. ESP_LOGE(TAG, "Failed to read response");
  555. }
  556. }
  557. }
  558. esp_http_client_close(client);
  559. // POST Request
  560. const char *post_data = "{\"field1\":\"value1\"}";
  561. esp_http_client_set_url(client, "http://httpbin.org/post");
  562. esp_http_client_set_method(client, HTTP_METHOD_POST);
  563. esp_http_client_set_header(client, "Content-Type", "application/json");
  564. err = esp_http_client_open(client, strlen(post_data));
  565. if (err != ESP_OK) {
  566. ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
  567. } else {
  568. int wlen = esp_http_client_write(client, post_data, strlen(post_data));
  569. if (wlen < 0) {
  570. ESP_LOGE(TAG, "Write failed");
  571. }
  572. content_length = esp_http_client_fetch_headers(client);
  573. if (content_length < 0) {
  574. ESP_LOGE(TAG, "HTTP client fetch headers failed");
  575. } else {
  576. int data_read = esp_http_client_read_response(client, output_buffer, MAX_HTTP_OUTPUT_BUFFER);
  577. if (data_read >= 0) {
  578. ESP_LOGI(TAG, "HTTP POST Status = %d, content_length = %d",
  579. esp_http_client_get_status_code(client),
  580. esp_http_client_get_content_length(client));
  581. ESP_LOG_BUFFER_HEX(TAG, output_buffer, strlen(output_buffer));
  582. } else {
  583. ESP_LOGE(TAG, "Failed to read response");
  584. }
  585. }
  586. }
  587. esp_http_client_cleanup(client);
  588. }
  589. static void http_partial_download(void)
  590. {
  591. esp_http_client_config_t config = {
  592. .url = "https://dl.espressif.com/dl/esp-idf/ci/esp_http_client_demo.txt",
  593. .event_handler = _http_event_handler,
  594. .crt_bundle_attach = esp_crt_bundle_attach,
  595. };
  596. esp_http_client_handle_t client = esp_http_client_init(&config);
  597. // Download a file excluding first 10 bytes
  598. esp_http_client_set_header(client, "Range", "bytes=10-");
  599. esp_err_t err = esp_http_client_perform(client);
  600. if (err == ESP_OK) {
  601. ESP_LOGI(TAG, "HTTP Status = %d, content_length = %d",
  602. esp_http_client_get_status_code(client),
  603. esp_http_client_get_content_length(client));
  604. } else {
  605. ESP_LOGE(TAG, "HTTP request failed: %s", esp_err_to_name(err));
  606. }
  607. // Download last 10 bytes of a file
  608. esp_http_client_set_header(client, "Range", "bytes=-10");
  609. err = esp_http_client_perform(client);
  610. if (err == ESP_OK) {
  611. ESP_LOGI(TAG, "HTTP Status = %d, content_length = %d",
  612. esp_http_client_get_status_code(client),
  613. esp_http_client_get_content_length(client));
  614. } else {
  615. ESP_LOGE(TAG, "HTTP request failed: %s", esp_err_to_name(err));
  616. }
  617. // Download 10 bytes from 11 to 20
  618. esp_http_client_set_header(client, "Range", "bytes=11-20");
  619. err = esp_http_client_perform(client);
  620. if (err == ESP_OK) {
  621. ESP_LOGI(TAG, "HTTP Status = %d, content_length = %d",
  622. esp_http_client_get_status_code(client),
  623. esp_http_client_get_content_length(client));
  624. } else {
  625. ESP_LOGE(TAG, "HTTP request failed: %s", esp_err_to_name(err));
  626. }
  627. esp_http_client_cleanup(client);
  628. }
  629. static void http_test_task(void *pvParameters)
  630. {
  631. http_rest_with_url();
  632. http_rest_with_hostname_path();
  633. #if CONFIG_ESP_HTTP_CLIENT_ENABLE_BASIC_AUTH
  634. http_auth_basic();
  635. http_auth_basic_redirect();
  636. #endif
  637. #if CONFIG_ESP_HTTP_CLIENT_ENABLE_DIGEST_AUTH
  638. http_auth_digest();
  639. #endif
  640. http_relative_redirect();
  641. http_absolute_redirect();
  642. https_with_url();
  643. https_with_hostname_path();
  644. http_redirect_to_https();
  645. http_download_chunk();
  646. http_perform_as_stream_reader();
  647. https_async();
  648. https_with_invalid_url();
  649. http_native_request();
  650. http_partial_download();
  651. ESP_LOGI(TAG, "Finish http example");
  652. vTaskDelete(NULL);
  653. }
  654. void app_main(void)
  655. {
  656. esp_err_t ret = nvs_flash_init();
  657. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  658. ESP_ERROR_CHECK(nvs_flash_erase());
  659. ret = nvs_flash_init();
  660. }
  661. ESP_ERROR_CHECK(ret);
  662. ESP_ERROR_CHECK(esp_netif_init());
  663. ESP_ERROR_CHECK(esp_event_loop_create_default());
  664. /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
  665. * Read "Establishing Wi-Fi or Ethernet Connection" section in
  666. * examples/protocols/README.md for more information about this function.
  667. */
  668. ESP_ERROR_CHECK(example_connect());
  669. ESP_LOGI(TAG, "Connected to AP, begin http example");
  670. xTaskCreate(&http_test_task, "http_test_task", 8192, NULL, 5, NULL);
  671. }