1 /* ESP HTTP Client Example
3 This example code is in the Public Domain (or CC0 licensed, at your option.)
5 Unless required by applicable law or agreed to in writing, this
6 software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
7 CONDITIONS OF ANY KIND, either express or implied.
12 #include "freertos/FreeRTOS.h"
13 #include "freertos/task.h"
15 #include "esp_system.h"
16 #include "nvs_flash.h"
19 #include "esp_http_client.h"
21 #define MAX_HTTP_RECV_BUFFER 512
22 static const char *TAG = "HTTP_CLIENT";
24 /* Root cert for howsmyssl.com, taken from howsmyssl_com_root_cert.pem
26 The PEM file was extracted from the output of this command:
27 openssl s_client -showcerts -connect www.howsmyssl.com:443 </dev/null
29 The CA root cert is the last cert given in the chain of certs.
31 To embed it in the app binary, the PEM file is named
32 in the component.mk COMPONENT_EMBED_TXTFILES variable.
34 extern const char howsmyssl_com_root_cert_pem_start[] asm("_binary_howsmyssl_com_root_cert_pem_start");
35 extern const char howsmyssl_com_root_cert_pem_end[] asm("_binary_howsmyssl_com_root_cert_pem_end");
37 esp_err_t _http_event_handler(esp_http_client_event_t *evt)
39 switch(evt->event_id) {
40 case HTTP_EVENT_ERROR:
41 ESP_LOGD(TAG, "HTTP_EVENT_ERROR");
43 case HTTP_EVENT_ON_CONNECTED:
44 ESP_LOGD(TAG, "HTTP_EVENT_ON_CONNECTED");
46 case HTTP_EVENT_HEADER_SENT:
47 ESP_LOGD(TAG, "HTTP_EVENT_HEADER_SENT");
49 case HTTP_EVENT_ON_HEADER:
50 ESP_LOGD(TAG, "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value);
52 case HTTP_EVENT_ON_DATA:
53 ESP_LOGD(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len);
54 if (!esp_http_client_is_chunked_response(evt->client)) {
56 // printf("%.*s", evt->data_len, (char*)evt->data);
60 case HTTP_EVENT_ON_FINISH:
61 ESP_LOGD(TAG, "HTTP_EVENT_ON_FINISH");
63 case HTTP_EVENT_DISCONNECTED:
64 ESP_LOGD(TAG, "HTTP_EVENT_DISCONNECTED");
70 static void http_rest()
72 esp_http_client_config_t config = {
73 .url = "http://httpbin.org/get",
74 .event_handler = _http_event_handler,
76 esp_http_client_handle_t client = esp_http_client_init(&config);
79 esp_err_t err = esp_http_client_perform(client);
81 ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %d",
82 esp_http_client_get_status_code(client),
83 esp_http_client_get_content_length(client));
85 ESP_LOGE(TAG, "HTTP GET request failed: %s", esp_err_to_name(err));
89 const char *post_data = "field1=value1&field2=value2";
90 esp_http_client_set_url(client, "http://httpbin.org/post");
91 esp_http_client_set_method(client, HTTP_METHOD_POST);
92 esp_http_client_set_post_field(client, post_data, strlen(post_data));
93 err = esp_http_client_perform(client);
95 ESP_LOGI(TAG, "HTTP POST Status = %d, content_length = %d",
96 esp_http_client_get_status_code(client),
97 esp_http_client_get_content_length(client));
99 ESP_LOGE(TAG, "HTTP POST request failed: %s", esp_err_to_name(err));
103 esp_http_client_set_url(client, "http://httpbin.org/put");
104 esp_http_client_set_method(client, HTTP_METHOD_PUT);
105 err = esp_http_client_perform(client);
107 ESP_LOGI(TAG, "HTTP PUT Status = %d, content_length = %d",
108 esp_http_client_get_status_code(client),
109 esp_http_client_get_content_length(client));
111 ESP_LOGE(TAG, "HTTP PUT request failed: %s", esp_err_to_name(err));
115 esp_http_client_set_url(client, "http://httpbin.org/patch");
116 esp_http_client_set_method(client, HTTP_METHOD_PATCH);
117 esp_http_client_set_post_field(client, NULL, 0);
118 err = esp_http_client_perform(client);
120 ESP_LOGI(TAG, "HTTP PATCH Status = %d, content_length = %d",
121 esp_http_client_get_status_code(client),
122 esp_http_client_get_content_length(client));
124 ESP_LOGE(TAG, "HTTP PATCH request failed: %s", esp_err_to_name(err));
128 esp_http_client_set_url(client, "http://httpbin.org/delete");
129 esp_http_client_set_method(client, HTTP_METHOD_DELETE);
130 err = esp_http_client_perform(client);
132 ESP_LOGI(TAG, "HTTP DELETE Status = %d, content_length = %d",
133 esp_http_client_get_status_code(client),
134 esp_http_client_get_content_length(client));
136 ESP_LOGE(TAG, "HTTP DELETE request failed: %s", esp_err_to_name(err));
139 esp_http_client_cleanup(client);
142 static void http_auth_basic()
144 esp_http_client_config_t config = {
145 .url = "http://user:passwd@httpbin.org/basic-auth/user/passwd",
146 .event_handler = _http_event_handler,
147 .auth_type = HTTP_AUTH_TYPE_BASIC,
149 esp_http_client_handle_t client = esp_http_client_init(&config);
150 esp_err_t err = esp_http_client_perform(client);
153 ESP_LOGI(TAG, "HTTP Basic Auth Status = %d, content_length = %d",
154 esp_http_client_get_status_code(client),
155 esp_http_client_get_content_length(client));
157 ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err));
159 esp_http_client_cleanup(client);
162 static void http_auth_basic_redirect()
164 esp_http_client_config_t config = {
165 .url = "http://user:passwd@httpbin.org/basic-auth/user/passwd",
166 .event_handler = _http_event_handler,
168 esp_http_client_handle_t client = esp_http_client_init(&config);
169 esp_err_t err = esp_http_client_perform(client);
172 ESP_LOGI(TAG, "HTTP Basic Auth redirect Status = %d, content_length = %d",
173 esp_http_client_get_status_code(client),
174 esp_http_client_get_content_length(client));
176 ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err));
178 esp_http_client_cleanup(client);
181 static void http_auth_digest()
183 esp_http_client_config_t config = {
184 .url = "http://user:passwd@httpbin.org/digest-auth/auth/user/passwd/MD5/never",
185 .event_handler = _http_event_handler,
187 esp_http_client_handle_t client = esp_http_client_init(&config);
188 esp_err_t err = esp_http_client_perform(client);
191 ESP_LOGI(TAG, "HTTP Digest Auth Status = %d, content_length = %d",
192 esp_http_client_get_status_code(client),
193 esp_http_client_get_content_length(client));
195 ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err));
197 esp_http_client_cleanup(client);
202 esp_http_client_config_t config = {
203 .url = "https://www.howsmyssl.com",
204 .event_handler = _http_event_handler,
205 .cert_pem = howsmyssl_com_root_cert_pem_start,
207 esp_http_client_handle_t client = esp_http_client_init(&config);
208 esp_err_t err = esp_http_client_perform(client);
211 ESP_LOGI(TAG, "HTTPS Status = %d, content_length = %d",
212 esp_http_client_get_status_code(client),
213 esp_http_client_get_content_length(client));
215 ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err));
217 esp_http_client_cleanup(client);
220 static void http_relative_redirect()
222 esp_http_client_config_t config = {
223 .url = "http://httpbin.org/relative-redirect/3",
224 .event_handler = _http_event_handler,
226 esp_http_client_handle_t client = esp_http_client_init(&config);
227 esp_err_t err = esp_http_client_perform(client);
230 ESP_LOGI(TAG, "HTTP Relative path redirect Status = %d, content_length = %d",
231 esp_http_client_get_status_code(client),
232 esp_http_client_get_content_length(client));
234 ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err));
236 esp_http_client_cleanup(client);
239 static void http_absolute_redirect()
241 esp_http_client_config_t config = {
242 .url = "http://httpbin.org/absolute-redirect/3",
243 .event_handler = _http_event_handler,
245 esp_http_client_handle_t client = esp_http_client_init(&config);
246 esp_err_t err = esp_http_client_perform(client);
249 ESP_LOGI(TAG, "HTTP Absolute path redirect Status = %d, content_length = %d",
250 esp_http_client_get_status_code(client),
251 esp_http_client_get_content_length(client));
253 ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err));
255 esp_http_client_cleanup(client);
258 static void http_redirect_to_https()
260 esp_http_client_config_t config = {
261 .url = "http://httpbin.org/redirect-to?url=https%3A%2F%2Fwww.howsmyssl.com",
262 .event_handler = _http_event_handler,
264 esp_http_client_handle_t client = esp_http_client_init(&config);
265 esp_err_t err = esp_http_client_perform(client);
268 ESP_LOGI(TAG, "HTTP redirect to HTTPS Status = %d, content_length = %d",
269 esp_http_client_get_status_code(client),
270 esp_http_client_get_content_length(client));
272 ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err));
274 esp_http_client_cleanup(client);
278 static void http_download_chunk()
280 esp_http_client_config_t config = {
281 .url = "http://httpbin.org/stream-bytes/8912",
282 .event_handler = _http_event_handler,
284 esp_http_client_handle_t client = esp_http_client_init(&config);
285 esp_err_t err = esp_http_client_perform(client);
288 ESP_LOGI(TAG, "HTTP chunk encoding Status = %d, content_length = %d",
289 esp_http_client_get_status_code(client),
290 esp_http_client_get_content_length(client));
292 ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err));
294 esp_http_client_cleanup(client);
297 static void http_perform_as_stream_reader()
299 char *buffer = malloc(MAX_HTTP_RECV_BUFFER + 1);
300 if (buffer == NULL) {
301 ESP_LOGE(TAG, "Cannot malloc http receive buffer");
304 esp_http_client_config_t config = {
305 .url = "http://httpbin.org/get",
306 .event_handler = _http_event_handler,
308 esp_http_client_handle_t client = esp_http_client_init(&config);
310 if ((err = esp_http_client_open(client, 0)) != ESP_OK) {
311 ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
315 int content_length = esp_http_client_fetch_headers(client);
316 int total_read_len = 0, read_len;
317 if (total_read_len < content_length && content_length <= MAX_HTTP_RECV_BUFFER) {
318 read_len = esp_http_client_read(client, buffer, content_length);
320 ESP_LOGE(TAG, "Error read data");
322 buffer[read_len] = 0;
323 ESP_LOGD(TAG, "read_len = %d", read_len);
325 ESP_LOGI(TAG, "HTTP Stream reader Status = %d, content_length = %d",
326 esp_http_client_get_status_code(client),
327 esp_http_client_get_content_length(client));
328 esp_http_client_close(client);
329 esp_http_client_cleanup(client);
333 static void http_test_task(void *pvParameters)
335 app_wifi_wait_connected();
336 ESP_LOGI(TAG, "Connected to AP, begin http example");
339 http_auth_basic_redirect();
341 http_relative_redirect();
342 http_absolute_redirect();
344 http_redirect_to_https();
345 http_download_chunk();
346 http_perform_as_stream_reader();
347 ESP_LOGI(TAG, "Finish http example");
353 esp_err_t ret = nvs_flash_init();
354 if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
355 ESP_ERROR_CHECK(nvs_flash_erase());
356 ret = nvs_flash_init();
358 ESP_ERROR_CHECK(ret);
359 app_wifi_initialise();
361 xTaskCreate(&http_test_task, "http_test_task", 8192, NULL, 5, NULL);