37 UserData *user_data = (UserData *) evt->user_data;
39 switch (evt->event_id) {
40 case HTTP_EVENT_ON_HEADER: {
43 const std::string header_value = evt->header_value;
44 ESP_LOGD(TAG,
"Received response header, name: %s, value: %s", header_name.c_str(), header_value.c_str());
45 user_data->response_headers.push_back({header_name, header_value});
57 const std::string &body,
58 const std::vector<Header> &request_headers,
59 const std::vector<std::string> &lower_case_collect_headers) {
62 ESP_LOGE(TAG,
"HTTP Request failed; Not connected to network");
66 esp_http_client_method_t method_idf;
67 if (method ==
"GET") {
68 method_idf = HTTP_METHOD_GET;
69 }
else if (method ==
"POST") {
70 method_idf = HTTP_METHOD_POST;
71 }
else if (method ==
"PUT") {
72 method_idf = HTTP_METHOD_PUT;
73 }
else if (method ==
"DELETE") {
74 method_idf = HTTP_METHOD_DELETE;
75 }
else if (method ==
"PATCH") {
76 method_idf = HTTP_METHOD_PATCH;
79 ESP_LOGE(TAG,
"HTTP Request failed; Unsupported method");
83 bool secure = url.find(
"https:") != std::string::npos;
85 esp_http_client_config_t config = {};
87 config.url = url.c_str();
88 config.method = method_idf;
92 config.auth_type = HTTP_AUTH_TYPE_BASIC;
96#if CONFIG_MBEDTLS_CERTIFICATE_BUNDLE
98 config.crt_bundle_attach = esp_crt_bundle_attach;
115 esp_http_client_handle_t client = esp_http_client_init(&config);
116 if (client ==
nullptr) {
118 ESP_LOGE(TAG,
"HTTP Request failed; client could not be initialized");
122 std::shared_ptr<HttpContainerIDF> container = std::make_shared<HttpContainerIDF>(client);
123 container->set_parent(
this);
125 container->set_secure(secure);
127 auto user_data = UserData{lower_case_collect_headers, container->response_headers_};
128 esp_http_client_set_user_data(client,
static_cast<void *
>(&user_data));
130 for (
const auto &header : request_headers) {
131 esp_http_client_set_header(client, header.name.c_str(), header.value.c_str());
134 const int body_len = body.length();
136 esp_err_t err = esp_http_client_open(client, body_len);
139 ESP_LOGE(TAG,
"HTTP Request failed: %s", esp_err_to_name(err));
140 esp_http_client_cleanup(client);
145 int write_left = body_len;
147 const char *buf = body.c_str();
148 while (write_left > 0) {
149 int written = esp_http_client_write(client, buf + write_index, write_left);
161 ESP_LOGE(TAG,
"HTTP Request failed: %s", esp_err_to_name(err));
162 esp_http_client_cleanup(client);
166 container->feed_wdt();
169 container->content_length = esp_http_client_fetch_headers(client);
170 container->set_chunked(esp_http_client_is_chunked_response(client));
171 container->feed_wdt();
172 container->status_code = esp_http_client_get_status_code(client);
173 container->feed_wdt();
181 while (
is_redirect(container->status_code) && num_redirects > 0) {
182 err = esp_http_client_set_redirection(client);
184 ESP_LOGE(TAG,
"esp_http_client_set_redirection failed: %s", esp_err_to_name(err));
186 esp_http_client_cleanup(client);
189#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
190 char redirect_url[256]{};
191 if (esp_http_client_get_url(client, redirect_url,
sizeof(redirect_url) - 1) == ESP_OK) {
192 ESP_LOGV(TAG,
"redirecting to url: %s", redirect_url);
195 err = esp_http_client_open(client, 0);
197 ESP_LOGE(TAG,
"esp_http_client_open failed: %s", esp_err_to_name(err));
199 esp_http_client_cleanup(client);
203 container->feed_wdt();
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.c_str(), container->status_code);