34 UserData *user_data = (UserData *) evt->user_data;
36 switch (evt->event_id) {
37 case HTTP_EVENT_ON_HEADER: {
39 if (user_data->collect_headers.count(header_name)) {
40 const std::string header_value = evt->header_value;
41 ESP_LOGD(TAG,
"Received response header, name: %s, value: %s", header_name.c_str(), header_value.c_str());
42 user_data->response_headers[header_name].push_back(header_value);
55 std::list<Header> request_headers,
56 std::set<std::string> collect_headers) {
59 ESP_LOGE(TAG,
"HTTP Request failed; Not connected to network");
63 esp_http_client_method_t method_idf;
64 if (method ==
"GET") {
65 method_idf = HTTP_METHOD_GET;
66 }
else if (method ==
"POST") {
67 method_idf = HTTP_METHOD_POST;
68 }
else if (method ==
"PUT") {
69 method_idf = HTTP_METHOD_PUT;
70 }
else if (method ==
"DELETE") {
71 method_idf = HTTP_METHOD_DELETE;
72 }
else if (method ==
"PATCH") {
73 method_idf = HTTP_METHOD_PATCH;
76 ESP_LOGE(TAG,
"HTTP Request failed; Unsupported method");
80 bool secure = url.find(
"https:") != std::string::npos;
82 esp_http_client_config_t config = {};
84 config.url = url.c_str();
85 config.method = method_idf;
89 config.auth_type = HTTP_AUTH_TYPE_BASIC;
90#if CONFIG_MBEDTLS_CERTIFICATE_BUNDLE
92 config.crt_bundle_attach = esp_crt_bundle_attach;
107 auto user_data = UserData{collect_headers, {}};
108 config.user_data =
static_cast<void *
>(&user_data);
110 esp_http_client_handle_t client = esp_http_client_init(&config);
112 std::shared_ptr<HttpContainerIDF> container = std::make_shared<HttpContainerIDF>(client);
113 container->set_parent(
this);
115 container->set_secure(secure);
117 for (
const auto &header : request_headers) {
118 esp_http_client_set_header(client, header.name.c_str(), header.value.c_str());
121 const int body_len = body.length();
123 esp_err_t err = esp_http_client_open(client, body_len);
126 ESP_LOGE(TAG,
"HTTP Request failed: %s", esp_err_to_name(err));
127 esp_http_client_cleanup(client);
132 int write_left = body_len;
134 const char *buf = body.c_str();
135 while (write_left > 0) {
136 int written = esp_http_client_write(client, buf + write_index, write_left);
141 write_left -= written;
142 write_index += written;
148 ESP_LOGE(TAG,
"HTTP Request failed: %s", esp_err_to_name(err));
149 esp_http_client_cleanup(client);
153 container->feed_wdt();
154 container->content_length = esp_http_client_fetch_headers(client);
155 container->feed_wdt();
156 container->status_code = esp_http_client_get_status_code(client);
157 container->feed_wdt();
158 container->set_response_headers(user_data.response_headers);
166 while (
is_redirect(container->status_code) && num_redirects > 0) {
167 err = esp_http_client_set_redirection(client);
169 ESP_LOGE(TAG,
"esp_http_client_set_redirection failed: %s", esp_err_to_name(err));
171 esp_http_client_cleanup(client);
174#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
175 char redirect_url[256]{};
176 if (esp_http_client_get_url(client, redirect_url,
sizeof(redirect_url) - 1) == ESP_OK) {
177 ESP_LOGV(TAG,
"redirecting to url: %s", redirect_url);
180 err = esp_http_client_open(client, 0);
182 ESP_LOGE(TAG,
"esp_http_client_open failed: %s", esp_err_to_name(err));
184 esp_http_client_cleanup(client);
188 container->feed_wdt();
189 container->content_length = esp_http_client_fetch_headers(client);
190 container->feed_wdt();
191 container->status_code = esp_http_client_get_status_code(client);
192 container->feed_wdt();
201 if (num_redirects == 0) {
206 ESP_LOGE(TAG,
"HTTP Request failed; URL: %s; Code: %d", url.c_str(), container->status_code);