esp_http_client: Fix config member path's incorrect setting issue

This commit is contained in:
Jitin George
2019-01-21 19:08:24 +08:00
committed by Ivan Grokhotkov
parent 5d7d4ba4f3
commit 4a667ee12c
5 changed files with 202 additions and 23 deletions

View File

@@ -67,7 +67,7 @@ esp_err_t _http_event_handler(esp_http_client_event_t *evt)
return ESP_OK;
}
static void http_rest()
static void http_rest_with_url()
{
esp_http_client_config_t config = {
.url = "http://httpbin.org/get",
@@ -151,6 +151,93 @@ static void http_rest()
esp_http_client_cleanup(client);
}
static void http_rest_with_hostname_path()
{
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);
}
static void http_auth_basic()
{
esp_http_client_config_t config = {
@@ -209,7 +296,7 @@ static void http_auth_digest()
esp_http_client_cleanup(client);
}
static void https()
static void https_with_url()
{
esp_http_client_config_t config = {
.url = "https://www.howsmyssl.com",
@@ -229,6 +316,28 @@ static void https()
esp_http_client_cleanup(client);
}
static void https_with_hostname_path()
{
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()
{
esp_http_client_config_t config = {
@@ -374,21 +483,25 @@ static void https_async()
esp_http_client_cleanup(client);
}
static void http_test_task(void *pvParameters)
{
app_wifi_wait_connected();
ESP_LOGI(TAG, "Connected to AP, begin http example");
http_rest();
http_rest_with_url();
http_rest_with_hostname_path();
http_auth_basic();
http_auth_basic_redirect();
http_auth_digest();
http_relative_redirect();
http_absolute_redirect();
https();
https_with_url();
https_with_hostname_path();
http_redirect_to_https();
http_download_chunk();
http_perform_as_stream_reader();
https_async();
ESP_LOGI(TAG, "Finish http example");
vTaskDelete(NULL);
}