36 UserData *user_data = (UserData *) evt->user_data;
38 switch (evt->event_id) {
39 case HTTP_EVENT_ON_HEADER: {
41 if (user_data->collect_headers.count(header_name)) {
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[header_name].push_back(header_value);
56 std::list<Header> request_headers,
57 std::set<std::string> collect_headers) {
60 ESP_LOGE(TAG,
"HTTP Request failed; Not connected to network");
64 esp_http_client_method_t method_idf;
65 if (method ==
"GET") {
66 method_idf = HTTP_METHOD_GET;
67 }
else if (method ==
"POST") {
68 method_idf = HTTP_METHOD_POST;
69 }
else if (method ==
"PUT") {
70 method_idf = HTTP_METHOD_PUT;
71 }
else if (method ==
"DELETE") {
72 method_idf = HTTP_METHOD_DELETE;
73 }
else if (method ==
"PATCH") {
74 method_idf = HTTP_METHOD_PATCH;
77 ESP_LOGE(TAG,
"HTTP Request failed; Unsupported method");
81 bool secure = url.find(
"https:") != std::string::npos;
83 esp_http_client_config_t config = {};
85 config.url = url.c_str();
86 config.method = method_idf;
90 config.auth_type = HTTP_AUTH_TYPE_BASIC;
91#if CONFIG_MBEDTLS_CERTIFICATE_BUNDLE
93 config.crt_bundle_attach = esp_crt_bundle_attach;
108 auto user_data = UserData{collect_headers, {}};
109 config.user_data =
static_cast<void *
>(&user_data);
111 esp_http_client_handle_t client = esp_http_client_init(&config);
113 std::shared_ptr<HttpContainerIDF> container = std::make_shared<HttpContainerIDF>(client);
114 container->set_parent(
this);
116 container->set_secure(secure);
118 for (
const auto &header : request_headers) {
119 esp_http_client_set_header(client, header.name.c_str(), header.value.c_str());
122 const int body_len = body.length();
124 esp_err_t err = esp_http_client_open(client, body_len);
127 ESP_LOGE(TAG,
"HTTP Request failed: %s", esp_err_to_name(err));
128 esp_http_client_cleanup(client);
133 int write_left = body_len;
135 const char *buf = body.c_str();
136 while (write_left > 0) {
137 int written = esp_http_client_write(client, buf + write_index, write_left);
142 write_left -= written;
143 write_index += written;
149 ESP_LOGE(TAG,
"HTTP Request failed: %s", esp_err_to_name(err));
150 esp_http_client_cleanup(client);
154 container->feed_wdt();
155 container->content_length = esp_http_client_fetch_headers(client);
156 container->feed_wdt();
157 container->status_code = esp_http_client_get_status_code(client);
158 container->feed_wdt();
159 container->set_response_headers(user_data.response_headers);
167 while (
is_redirect(container->status_code) && num_redirects > 0) {
168 err = esp_http_client_set_redirection(client);
170 ESP_LOGE(TAG,
"esp_http_client_set_redirection failed: %s", esp_err_to_name(err));
172 esp_http_client_cleanup(client);
175#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
176 char redirect_url[256]{};
177 if (esp_http_client_get_url(client, redirect_url,
sizeof(redirect_url) - 1) == ESP_OK) {
178 ESP_LOGV(TAG,
"redirecting to url: %s", redirect_url);
181 err = esp_http_client_open(client, 0);
183 ESP_LOGE(TAG,
"esp_http_client_open failed: %s", esp_err_to_name(err));
185 esp_http_client_cleanup(client);
189 container->feed_wdt();
190 container->content_length = esp_http_client_fetch_headers(client);
191 container->feed_wdt();
192 container->status_code = esp_http_client_get_status_code(client);
193 container->feed_wdt();
202 if (num_redirects == 0) {
207 ESP_LOGE(TAG,
"HTTP Request failed; URL: %s; Code: %d", url.c_str(), container->status_code);