52 const std::string &body,
53 const std::vector<Header> &request_headers,
54 const std::vector<std::string> &lower_case_collect_headers) {
57 ESP_LOGE(TAG,
"HTTP Request failed; Not connected to network");
61 esp_http_client_method_t method_idf;
62 if (method ==
"GET") {
63 method_idf = HTTP_METHOD_GET;
64 }
else if (method ==
"POST") {
65 method_idf = HTTP_METHOD_POST;
66 }
else if (method ==
"PUT") {
67 method_idf = HTTP_METHOD_PUT;
68 }
else if (method ==
"DELETE") {
69 method_idf = HTTP_METHOD_DELETE;
70 }
else if (method ==
"PATCH") {
71 method_idf = HTTP_METHOD_PATCH;
74 ESP_LOGE(TAG,
"HTTP Request failed; Unsupported method");
78 bool secure = url.find(
"https:") != std::string::npos;
80 esp_http_client_config_t config = {};
82 config.url = url.c_str();
83 config.method = method_idf;
87 config.auth_type = HTTP_AUTH_TYPE_BASIC;
91#if CONFIG_MBEDTLS_CERTIFICATE_BUNDLE
93 config.crt_bundle_attach = esp_crt_bundle_attach;
110 esp_http_client_handle_t client = esp_http_client_init(&config);
111 if (client ==
nullptr) {
113 ESP_LOGE(TAG,
"HTTP Request failed; client could not be initialized");
117 std::shared_ptr<HttpContainerIDF> container = std::make_shared<HttpContainerIDF>(client);
118 container->set_parent(
this);
120 container->set_secure(secure);
122 container->collect_headers_ = lower_case_collect_headers;
123 esp_http_client_set_user_data(client,
static_cast<void *
>(container.get()));
125 for (
const auto &header : request_headers) {
126 esp_http_client_set_header(client, header.name.c_str(), header.value.c_str());
129 const int body_len = body.length();
131 esp_err_t err = esp_http_client_open(client, body_len);
134 ESP_LOGE(TAG,
"HTTP Request failed: %s", esp_err_to_name(err));
135 esp_http_client_cleanup(client);
140 int write_left = body_len;
142 const char *buf = body.c_str();
143 while (write_left > 0) {
144 int written = esp_http_client_write(client, buf + write_index, write_left);
156 ESP_LOGE(TAG,
"HTTP Request failed: %s", esp_err_to_name(err));
157 esp_http_client_cleanup(client);
161 container->feed_wdt();
164 container->content_length = esp_http_client_fetch_headers(client);
165 container->set_chunked(esp_http_client_is_chunked_response(client));
166 container->feed_wdt();
167 container->status_code = esp_http_client_get_status_code(client);
168 container->feed_wdt();
176 while (
is_redirect(container->status_code) && num_redirects > 0) {
177 err = esp_http_client_set_redirection(client);
179 ESP_LOGE(TAG,
"esp_http_client_set_redirection failed: %s", esp_err_to_name(err));
181 esp_http_client_cleanup(client);
184#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
185 char redirect_url[256]{};
186 if (esp_http_client_get_url(client, redirect_url,
sizeof(redirect_url) - 1) == ESP_OK) {
187 ESP_LOGV(TAG,
"redirecting to url: %s", redirect_url);
190 err = esp_http_client_open(client, 0);
192 ESP_LOGE(TAG,
"esp_http_client_open failed: %s", esp_err_to_name(err));
194 esp_http_client_cleanup(client);
198 container->feed_wdt();
199 container->content_length = esp_http_client_fetch_headers(client);
200 container->set_chunked(esp_http_client_is_chunked_response(client));
201 container->feed_wdt();
202 container->status_code = esp_http_client_get_status_code(client);
203 container->feed_wdt();
212 if (num_redirects == 0) {
217 ESP_LOGE(TAG,
"HTTP Request failed; URL: %s; Code: %d", url.c_str(), container->status_code);