12 #if CONFIG_MBEDTLS_CERTIFICATE_BUNDLE 13 #include "esp_crt_bundle.h" 17 namespace http_request {
19 static const char *
const TAG =
"http_request.idf";
28 std::list<Header> headers) {
31 ESP_LOGE(TAG,
"HTTP Request failed; Not connected to network");
35 esp_http_client_method_t method_idf;
36 if (method ==
"GET") {
37 method_idf = HTTP_METHOD_GET;
38 }
else if (method ==
"POST") {
39 method_idf = HTTP_METHOD_POST;
40 }
else if (method ==
"PUT") {
41 method_idf = HTTP_METHOD_PUT;
42 }
else if (method ==
"DELETE") {
43 method_idf = HTTP_METHOD_DELETE;
44 }
else if (method ==
"PATCH") {
45 method_idf = HTTP_METHOD_PATCH;
48 ESP_LOGE(TAG,
"HTTP Request failed; Unsupported method");
52 bool secure = url.find(
"https:") != std::string::npos;
54 esp_http_client_config_t config = {};
56 config.url = url.c_str();
57 config.method = method_idf;
61 config.auth_type = HTTP_AUTH_TYPE_BASIC;
62 #if CONFIG_MBEDTLS_CERTIFICATE_BUNDLE 64 config.crt_bundle_attach = esp_crt_bundle_attach;
78 esp_http_client_handle_t client = esp_http_client_init(&config);
80 std::shared_ptr<HttpContainerIDF> container = std::make_shared<HttpContainerIDF>(client);
81 container->set_parent(
this);
83 container->set_secure(secure);
85 for (
const auto &header : headers) {
86 esp_http_client_set_header(client, header.name, header.value);
89 const int body_len = body.length();
91 esp_err_t err = esp_http_client_open(client, body_len);
94 ESP_LOGE(TAG,
"HTTP Request failed: %s", esp_err_to_name(err));
95 esp_http_client_cleanup(client);
100 int write_left = body_len;
102 const char *buf = body.c_str();
103 while (write_left > 0) {
104 int written = esp_http_client_write(client, buf + write_index, write_left);
109 write_left -= written;
110 write_index += written;
116 ESP_LOGE(TAG,
"HTTP Request failed: %s", esp_err_to_name(err));
117 esp_http_client_cleanup(client);
121 auto is_ok = [](
int code) {
return code >= HttpStatus_Ok && code < HttpStatus_MultipleChoices; };
123 container->content_length = esp_http_client_fetch_headers(client);
124 container->status_code = esp_http_client_get_status_code(client);
125 if (is_ok(container->status_code)) {
131 auto is_redirect = [](
int code) {
132 return code == HttpStatus_MovedPermanently || code == HttpStatus_Found || code == HttpStatus_SeeOther ||
133 code == HttpStatus_TemporaryRedirect || code == HttpStatus_PermanentRedirect;
136 while (is_redirect(container->status_code) && num_redirects > 0) {
137 err = esp_http_client_set_redirection(client);
139 ESP_LOGE(TAG,
"esp_http_client_set_redirection failed: %s", esp_err_to_name(err));
141 esp_http_client_cleanup(client);
144 #if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE 146 if (esp_http_client_get_url(client, url,
sizeof(url) - 1) == ESP_OK) {
147 ESP_LOGV(TAG,
"redirecting to url: %s", url);
150 err = esp_http_client_open(client, 0);
152 ESP_LOGE(TAG,
"esp_http_client_open failed: %s", esp_err_to_name(err));
154 esp_http_client_cleanup(client);
158 container->content_length = esp_http_client_fetch_headers(client);
159 container->status_code = esp_http_client_get_status_code(client);
160 if (is_ok(container->status_code)) {
168 if (num_redirects == 0) {
173 ESP_LOGE(TAG,
"HTTP Request failed; URL: %s; Code: %d", url.c_str(), container->status_code);
175 esp_http_client_cleanup(client);
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);
192 this->bytes_read_ += read_len;
202 esp_http_client_close(this->client_);
203 esp_http_client_cleanup(this->client_);
209 #endif // USE_ESP_IDF
uint32_t get_watchdog_timeout() const
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)
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