examples/protocols/esp_http_client: Add example for asynchronous HTTP request

This commit is contained in:
Jitin George 2018-08-29 16:23:48 +05:30 committed by bot
parent 5309ca04d1
commit 0aec63c18e
2 changed files with 38 additions and 9 deletions

View file

@ -33,8 +33,6 @@ static const char *TAG = "esp-tls";
#define ESP_LOGE(TAG, ...) printf(__VA_ARGS__); #define ESP_LOGE(TAG, ...) printf(__VA_ARGS__);
#endif #endif
#define DEFAULT_TIMEOUT_MS 0
static struct addrinfo *resolve_host_name(const char *host, size_t hostlen) static struct addrinfo *resolve_host_name(const char *host, size_t hostlen)
{ {
struct addrinfo hints; 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->wset = tls->rset;
} }
tls->conn_state = ESP_TLS_CONNECTING; tls->conn_state = ESP_TLS_CONNECTING;
case ESP_TLS_CONNECTING: /* fall-through */ /* falls through */
case ESP_TLS_CONNECTING:
if (cfg->non_block) { if (cfg->non_block) {
ESP_LOGD(TAG, "connecting..."); ESP_LOGD(TAG, "connecting...");
struct timeval tv; struct timeval tv;
if (cfg->timeout_ms) { ms_to_timeval(cfg->timeout_ms, &tv);
ms_to_timeval(cfg->timeout_ms, &tv);
} else {
ms_to_timeval(DEFAULT_TIMEOUT_MS, &tv);
}
/* In case of non-blocking I/O, we use the select() API to check whether /* In case of non-blocking I/O, we use the select() API to check whether
connection has been estbalished or not*/ 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->read = tls_read;
tls->write = tls_write; tls->write = tls_write;
tls->conn_state = ESP_TLS_HANDSHAKE; tls->conn_state = ESP_TLS_HANDSHAKE;
case ESP_TLS_HANDSHAKE: /* fall-through */ /* falls through */
case ESP_TLS_HANDSHAKE:
ESP_LOGD(TAG, "handshake in progress..."); ESP_LOGD(TAG, "handshake in progress...");
ret = mbedtls_ssl_handshake(&tls->ssl); ret = mbedtls_ssl_handshake(&tls->ssl);
if (ret == 0) { if (ret == 0) {

View file

@ -342,6 +342,38 @@ static void http_perform_as_stream_reader()
free(buffer); 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) static void http_test_task(void *pvParameters)
{ {
app_wifi_wait_connected(); app_wifi_wait_connected();
@ -356,6 +388,7 @@ static void http_test_task(void *pvParameters)
http_redirect_to_https(); http_redirect_to_https();
http_download_chunk(); http_download_chunk();
http_perform_as_stream_reader(); http_perform_as_stream_reader();
https_async();
ESP_LOGI(TAG, "Finish http example"); ESP_LOGI(TAG, "Finish http example");
vTaskDelete(NULL); vTaskDelete(NULL);
} }