54 const std::vector<Header> &request_headers,
55 const std::vector<std::string> &lower_case_collect_headers) {
58 ESP_LOGE(TAG,
"HTTP Request failed; Not connected to network");
62 esp_http_client_method_t method_idf;
63 if (strcmp(method,
"GET") == 0) {
64 method_idf = HTTP_METHOD_GET;
65 }
else if (strcmp(method,
"POST") == 0) {
66 method_idf = HTTP_METHOD_POST;
67 }
else if (strcmp(method,
"PUT") == 0) {
68 method_idf = HTTP_METHOD_PUT;
69 }
else if (strcmp(method,
"DELETE") == 0) {
70 method_idf = HTTP_METHOD_DELETE;
71 }
else if (strcmp(method,
"PATCH") == 0) {
72 method_idf = HTTP_METHOD_PATCH;
75 ESP_LOGE(TAG,
"HTTP Request failed; Unsupported method");
79 bool secure = strstr(url,
"https:") !=
nullptr;
81 esp_http_client_config_t config = {};
84 config.method = method_idf;
88 config.auth_type = HTTP_AUTH_TYPE_BASIC;
92#if CONFIG_MBEDTLS_CERTIFICATE_BUNDLE
94 config.crt_bundle_attach = esp_crt_bundle_attach;
111 esp_http_client_handle_t client = esp_http_client_init(&config);
112 if (client ==
nullptr) {
114 ESP_LOGE(TAG,
"HTTP Request failed; client could not be initialized");
118 std::shared_ptr<HttpContainerIDF> container = std::make_shared<HttpContainerIDF>(client);
119 container->set_parent(
this);
121 container->set_secure(secure);
123 container->collect_headers_ = lower_case_collect_headers;
124 esp_http_client_set_user_data(client,
static_cast<void *
>(container.get()));
126 for (
const auto &header : request_headers) {
127 esp_http_client_set_header(client, header.name.c_str(), header.value.c_str());
130 const int body_len = body.length();
132 esp_err_t err = esp_http_client_open(client, body_len);
135 ESP_LOGE(TAG,
"HTTP Request failed: %s", esp_err_to_name(err));
136 esp_http_client_cleanup(client);
141 int write_left = body_len;
143 const char *buf = body.c_str();
144 while (write_left > 0) {
145 int written = esp_http_client_write(client, buf + write_index, write_left);
152 container->feed_wdt();
158 ESP_LOGE(TAG,
"HTTP Request failed: %s", esp_err_to_name(err));
159 esp_http_client_cleanup(client);
163 container->feed_wdt();
166 container->content_length = esp_http_client_fetch_headers(client);
167 container->set_chunked(esp_http_client_is_chunked_response(client));
168 container->feed_wdt();
169 container->status_code = esp_http_client_get_status_code(client);
170 container->feed_wdt();
178 while (
is_redirect(container->status_code) && num_redirects > 0) {
179 err = esp_http_client_set_redirection(client);
181 ESP_LOGE(TAG,
"esp_http_client_set_redirection failed: %s", esp_err_to_name(err));
183 esp_http_client_cleanup(client);
186#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
187 char redirect_url[256]{};
188 if (esp_http_client_get_url(client, redirect_url,
sizeof(redirect_url) - 1) == ESP_OK) {
189 ESP_LOGV(TAG,
"redirecting to url: %s", redirect_url);
192 err = esp_http_client_open(client, 0);
194 ESP_LOGE(TAG,
"esp_http_client_open failed: %s", esp_err_to_name(err));
196 esp_http_client_cleanup(client);
200 container->feed_wdt();
203 container->response_headers_.clear();
204 container->content_length = esp_http_client_fetch_headers(client);
205 container->set_chunked(esp_http_client_is_chunked_response(client));
206 container->feed_wdt();
207 container->status_code = esp_http_client_get_status_code(client);
208 container->feed_wdt();
217 if (num_redirects == 0) {
222 ESP_LOGE(TAG,
"HTTP Request failed; URL: %s; Code: %d", url, container->status_code);