11 #if CONFIG_MBEDTLS_CERTIFICATE_BUNDLE 12 #include "esp_crt_bundle.h" 15 #include "esp_task_wdt.h" 18 namespace http_request {
20 static const char *
const TAG =
"http_request.idf";
29 std::list<Header> headers) {
32 ESP_LOGE(TAG,
"HTTP Request failed; Not connected to network");
36 esp_http_client_method_t method_idf;
37 if (method ==
"GET") {
38 method_idf = HTTP_METHOD_GET;
39 }
else if (method ==
"POST") {
40 method_idf = HTTP_METHOD_POST;
41 }
else if (method ==
"PUT") {
42 method_idf = HTTP_METHOD_PUT;
43 }
else if (method ==
"DELETE") {
44 method_idf = HTTP_METHOD_DELETE;
45 }
else if (method ==
"PATCH") {
46 method_idf = HTTP_METHOD_PATCH;
49 ESP_LOGE(TAG,
"HTTP Request failed; Unsupported method");
53 bool secure = url.find(
"https:") != std::string::npos;
55 esp_http_client_config_t config = {};
57 config.url = url.c_str();
58 config.method = method_idf;
62 config.auth_type = HTTP_AUTH_TYPE_BASIC;
63 #if CONFIG_MBEDTLS_CERTIFICATE_BUNDLE 65 config.crt_bundle_attach = esp_crt_bundle_attach;
79 esp_http_client_handle_t client = esp_http_client_init(&config);
81 std::shared_ptr<HttpContainerIDF> container = std::make_shared<HttpContainerIDF>(client);
82 container->set_parent(
this);
84 container->set_secure(secure);
86 for (
const auto &header : headers) {
87 esp_http_client_set_header(client, header.name, header.value);
90 const int body_len = body.length();
92 esp_err_t err = esp_http_client_open(client, body_len);
95 ESP_LOGE(TAG,
"HTTP Request failed: %s", esp_err_to_name(err));
96 esp_http_client_cleanup(client);
101 int write_left = body_len;
103 const char *buf = body.c_str();
104 while (write_left > 0) {
105 int written = esp_http_client_write(client, buf + write_index, write_left);
110 write_left -= written;
111 write_index += written;
117 ESP_LOGE(TAG,
"HTTP Request failed: %s", esp_err_to_name(err));
118 esp_http_client_cleanup(client);
122 container->feed_wdt();
123 container->content_length = esp_http_client_fetch_headers(client);
124 container->feed_wdt();
125 container->status_code = esp_http_client_get_status_code(client);
126 container->feed_wdt();
134 while (
is_redirect(container->status_code) && num_redirects > 0) {
135 err = esp_http_client_set_redirection(client);
137 ESP_LOGE(TAG,
"esp_http_client_set_redirection failed: %s", esp_err_to_name(err));
139 esp_http_client_cleanup(client);
142 #if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE 143 char redirect_url[256]{};
144 if (esp_http_client_get_url(client, redirect_url,
sizeof(redirect_url) - 1) == ESP_OK) {
145 ESP_LOGV(TAG,
"redirecting to url: %s", redirect_url);
148 err = esp_http_client_open(client, 0);
150 ESP_LOGE(TAG,
"esp_http_client_open failed: %s", esp_err_to_name(err));
152 esp_http_client_cleanup(client);
156 container->feed_wdt();
157 container->content_length = esp_http_client_fetch_headers(client);
158 container->feed_wdt();
159 container->status_code = esp_http_client_get_status_code(client);
160 container->feed_wdt();
169 if (num_redirects == 0) {
174 ESP_LOGE(TAG,
"HTTP Request failed; URL: %s; Code: %d", url.c_str(), container->status_code);
183 int bufsize = std::min(max_len, this->content_length - this->bytes_read_);
191 int read_len = esp_http_client_read(this->client_, (
char *) buf, bufsize);
193 this->bytes_read_ += read_len;
203 esp_http_client_close(this->client_);
204 esp_http_client_cleanup(this->client_);
209 if (esp_task_wdt_status(
nullptr) == ESP_OK) {
217 #endif // USE_ESP_IDF
uint32_t get_watchdog_timeout() const
bool is_redirect(int const status)
Returns true if the HTTP status code is a redirect.
std::shared_ptr< HttpContainer > start(std::string url, std::string method, std::string body, std::list< Header > headers) override
int read(uint8_t *buf, size_t max_len) override
bool is_connected()
Return whether the node is connected to the network (through wifi, eth, ...)
uint32_t IRAM_ATTR HOT millis()
void status_momentary_error(const std::string &name, uint32_t length=5000)
void feed_wdt()
Feeds the watchdog timer if the executing task has one attached.
bool is_success(int const status)
Checks if the given HTTP status code indicates a successful request.
Application App
Global storage of Application pointer - only one Application can exist.
void dump_config() override
Implementation of SPI Controller mode.
void dump_config() override