]> granicus.if.org Git - esp-idf/commitdiff
examples/protocols/esp_http_client: Add example for asynchronous HTTP request
authorJitin George <jitin@espressif.com>
Wed, 29 Aug 2018 10:53:48 +0000 (16:23 +0530)
committerbot <bot@espressif.com>
Mon, 24 Sep 2018 05:42:28 +0000 (05:42 +0000)
components/esp-tls/esp_tls.c
examples/protocols/esp_http_client/main/esp_http_client_example.c

index 5abefaf34414c0ff04fab9c258e967e4559ef17d..3ead8ef2e5e361184068d8b3547298d618209a4b 100644 (file)
@@ -33,8 +33,6 @@ static const char *TAG = "esp-tls";
 #define ESP_LOGE(TAG, ...) printf(__VA_ARGS__);
 #endif
 
-#define DEFAULT_TIMEOUT_MS 0
-
 static struct addrinfo *resolve_host_name(const char *host, size_t hostlen)
 {
     struct addrinfo hints;
@@ -301,15 +299,12 @@ static int esp_tls_low_level_conn(const char *hostname, int hostlen, int port, c
                 tls->wset = tls->rset;
             }
             tls->conn_state = ESP_TLS_CONNECTING;
-        case ESP_TLS_CONNECTING:                                            /* fall-through */
+            /* falls through */
+        case ESP_TLS_CONNECTING:
             if (cfg->non_block) {
                 ESP_LOGD(TAG, "connecting...");
                 struct timeval tv;
-                if (cfg->timeout_ms) {
-                    ms_to_timeval(cfg->timeout_ms, &tv);
-                } else {
-                    ms_to_timeval(DEFAULT_TIMEOUT_MS, &tv);
-                }
+                ms_to_timeval(cfg->timeout_ms, &tv);
 
                 /* In case of non-blocking I/O, we use the select() API to check whether
                    connection has been estbalished or not*/
@@ -339,7 +334,8 @@ static int esp_tls_low_level_conn(const char *hostname, int hostlen, int port, c
             tls->read = tls_read;
             tls->write = tls_write;
             tls->conn_state = ESP_TLS_HANDSHAKE;
-        case ESP_TLS_HANDSHAKE:                                                  /* fall-through */
+            /* falls through */
+        case ESP_TLS_HANDSHAKE:
             ESP_LOGD(TAG, "handshake in progress...");
             ret = mbedtls_ssl_handshake(&tls->ssl);
             if (ret == 0) {
index f862ddc7002fa279c237549f1d1e304a91f00abd..ac5e1942c5612dda2bf2656bd19d44c58934d4c0 100644 (file)
@@ -342,6 +342,38 @@ static void http_perform_as_stream_reader()
     free(buffer);
 }
 
+static void https_async()
+{
+    esp_http_client_config_t config = {
+        .url = "https://postman-echo.com/post",
+        .event_handler = _http_event_handler,
+        .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 http_test_task(void *pvParameters)
 {
     app_wifi_wait_connected();
@@ -356,6 +388,7 @@ static void http_test_task(void *pvParameters)
     http_redirect_to_https();
     http_download_chunk();
     http_perform_as_stream_reader();
+    https_async();
     ESP_LOGI(TAG, "Finish http example");
     vTaskDelete(NULL);
 }