36 UserData *user_data = (UserData *) evt->user_data;
38 switch (evt->event_id) {
39 case HTTP_EVENT_ON_HEADER: {
42 const std::string header_value = evt->header_value;
43 ESP_LOGD(TAG,
"Received response header, name: %s, value: %s", header_name.c_str(), header_value.c_str());
44 user_data->response_headers.push_back({header_name, header_value});
56 const std::string &body,
57 const std::vector<Header> &request_headers,
58 const std::vector<std::string> &lower_case_collect_headers) {
61 ESP_LOGE(TAG,
"HTTP Request failed; Not connected to network");
65 esp_http_client_method_t method_idf;
66 if (method ==
"GET") {
67 method_idf = HTTP_METHOD_GET;
68 }
else if (method ==
"POST") {
69 method_idf = HTTP_METHOD_POST;
70 }
else if (method ==
"PUT") {
71 method_idf = HTTP_METHOD_PUT;
72 }
else if (method ==
"DELETE") {
73 method_idf = HTTP_METHOD_DELETE;
74 }
else if (method ==
"PATCH") {
75 method_idf = HTTP_METHOD_PATCH;
78 ESP_LOGE(TAG,
"HTTP Request failed; Unsupported method");
82 bool secure = url.find(
"https:") != std::string::npos;
84 esp_http_client_config_t config = {};
86 config.url = url.c_str();
87 config.method = method_idf;
91 config.auth_type = HTTP_AUTH_TYPE_BASIC;
95#if CONFIG_MBEDTLS_CERTIFICATE_BUNDLE
97 config.crt_bundle_attach = esp_crt_bundle_attach;
114 esp_http_client_handle_t client = esp_http_client_init(&config);
116 std::shared_ptr<HttpContainerIDF> container = std::make_shared<HttpContainerIDF>(client);
117 container->set_parent(
this);
119 container->set_secure(secure);
121 auto user_data = UserData{lower_case_collect_headers, container->response_headers_};
122 esp_http_client_set_user_data(client,
static_cast<void *
>(&user_data));
124 for (
const auto &header : request_headers) {
125 esp_http_client_set_header(client, header.name.c_str(), header.value.c_str());
128 const int body_len = body.length();
130 esp_err_t err = esp_http_client_open(client, body_len);
133 ESP_LOGE(TAG,
"HTTP Request failed: %s", esp_err_to_name(err));
134 esp_http_client_cleanup(client);
139 int write_left = body_len;
141 const char *buf = body.c_str();
142 while (write_left > 0) {
143 int written = esp_http_client_write(client, buf + write_index, write_left);
155 ESP_LOGE(TAG,
"HTTP Request failed: %s", esp_err_to_name(err));
156 esp_http_client_cleanup(client);
160 container->feed_wdt();
163 container->content_length = esp_http_client_fetch_headers(client);
164 container->set_chunked(esp_http_client_is_chunked_response(client));
165 container->feed_wdt();
166 container->status_code = esp_http_client_get_status_code(client);
167 container->feed_wdt();
175 while (
is_redirect(container->status_code) && num_redirects > 0) {
176 err = esp_http_client_set_redirection(client);
178 ESP_LOGE(TAG,
"esp_http_client_set_redirection failed: %s", esp_err_to_name(err));
180 esp_http_client_cleanup(client);
183#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
184 char redirect_url[256]{};
185 if (esp_http_client_get_url(client, redirect_url,
sizeof(redirect_url) - 1) == ESP_OK) {
186 ESP_LOGV(TAG,
"redirecting to url: %s", redirect_url);
189 err = esp_http_client_open(client, 0);
191 ESP_LOGE(TAG,
"esp_http_client_open failed: %s", esp_err_to_name(err));
193 esp_http_client_cleanup(client);
197 container->feed_wdt();
198 container->content_length = esp_http_client_fetch_headers(client);
199 container->set_chunked(esp_http_client_is_chunked_response(client));
200 container->feed_wdt();
201 container->status_code = esp_http_client_get_status_code(client);
202 container->feed_wdt();
211 if (num_redirects == 0) {
216 ESP_LOGE(TAG,
"HTTP Request failed; URL: %s; Code: %d", url.c_str(), container->status_code);