if (!_success) {
ESP_LOGE(TAG, "Error allocate memory");
- esp_http_client_cleanup(client);
- return NULL;
+ goto error;
}
_success = (
);
if (!_success) {
ESP_LOGE(TAG, "Error initialize transport");
- esp_http_client_cleanup(client);
- return NULL;
+ goto error;
}
#ifdef CONFIG_ESP_HTTP_CLIENT_ENABLE_HTTPS
esp_transport_handle_t ssl;
if (!_success) {
ESP_LOGE(TAG, "Error initialize SSL Transport");
- esp_http_client_cleanup(client);
- return NULL;
+ goto error;
}
if (config->cert_pem) {
if (_set_config(client, config) != ESP_OK) {
ESP_LOGE(TAG, "Error set configurations");
- esp_http_client_cleanup(client);
- return NULL;
+ goto error;
}
_success = (
(client->request->buffer->data = malloc(client->buffer_size)) &&
if (!_success) {
ESP_LOGE(TAG, "Allocation failed");
- esp_http_client_cleanup(client);
- return NULL;
+ goto error;
}
- _success = (
- (esp_http_client_set_url(client, config->url) == ESP_OK) &&
- (esp_http_client_set_header(client, "User-Agent", DEFAULT_HTTP_USER_AGENT) == ESP_OK) &&
- (esp_http_client_set_header(client, "Host", client->connection_info.host) == ESP_OK)
- );
+ if (config->host != NULL && config->path != NULL) {
+ _success = (
+ (esp_http_client_set_header(client, "User-Agent", DEFAULT_HTTP_USER_AGENT) == ESP_OK) &&
+ (esp_http_client_set_header(client, "Host", client->connection_info.host) == ESP_OK)
+ );
- if (!_success) {
- ESP_LOGE(TAG, "Error set default configurations");
- esp_http_client_cleanup(client);
- return NULL;
+ if (!_success) {
+ ESP_LOGE(TAG, "Error while setting default configurations");
+ goto error;
+ }
+ } else if (config->url != NULL) {
+ _success = (
+ (esp_http_client_set_url(client, config->url) == ESP_OK) &&
+ (esp_http_client_set_header(client, "User-Agent", DEFAULT_HTTP_USER_AGENT) == ESP_OK) &&
+ (esp_http_client_set_header(client, "Host", client->connection_info.host) == ESP_OK)
+ );
+
+ if (!_success) {
+ ESP_LOGE(TAG, "Error while setting default configurations");
+ goto error;
+ }
+ } else {
+ ESP_LOGE(TAG, "config should have either URL or host & path");
+ goto error;
}
client->parser_settings->on_message_begin = http_on_message_begin;
client->state = HTTP_STATE_INIT;
return client;
+error:
+ esp_http_client_cleanup(client);
+ return NULL;
}
esp_err_t esp_http_client_cleanup(esp_http_client_handle_t client)
--- /dev/null
+// Copyright 2018 Espressif Systems (Shanghai) PTE LTD
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include <stdlib.h>
+#include <stdbool.h>
+#include <esp_system.h>
+#include <esp_http_client.h>
+
+#include "unity.h"
+#include "test_utils.h"
+
+TEST_CASE("Input Param Tests", "[ESP HTTP CLIENT]")
+{
+ esp_http_client_config_t config_incorrect = {0};
+
+ test_case_uses_tcpip();
+
+ esp_http_client_handle_t client = esp_http_client_init(&config_incorrect);
+ TEST_ASSERT(client == NULL);
+
+ esp_http_client_config_t config_with_url = {
+ .url = "http://httpbin.org/get",
+ };
+ client = esp_http_client_init(&config_with_url);
+ TEST_ASSERT(client != NULL);
+ TEST_ASSERT(esp_http_client_cleanup(client) == ESP_OK);
+
+
+ esp_http_client_config_t config_with_hostname_path = {
+ .host = "httpbin.org",
+ .path = "/get",
+ };
+ client = esp_http_client_init(&config_with_hostname_path);
+ TEST_ASSERT(client != NULL);
+ TEST_ASSERT(esp_http_client_cleanup(client) == ESP_OK);
+}
return ESP_OK;
}
-static void http_rest()
+static void http_rest_with_url()
{
esp_http_client_config_t config = {
.url = "http://httpbin.org/get",
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 = {
esp_http_client_cleanup(client);
}
-static void https()
+static void https_with_url()
{
esp_http_client_config_t config = {
.url = "https://www.howsmyssl.com",
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 = {
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);
}