11 #if CONFIG_MBEDTLS_CERTIFICATE_BUNDLE 12 #include "esp_crt_bundle.h" 16 namespace http_request {
18 static const char *
const TAG =
"http_request.idf";
27 std::list<Header> headers) {
30 ESP_LOGE(TAG,
"HTTP Request failed; Not connected to network");
34 esp_http_client_method_t method_idf;
35 if (method ==
"GET") {
36 method_idf = HTTP_METHOD_GET;
37 }
else if (method ==
"POST") {
38 method_idf = HTTP_METHOD_POST;
39 }
else if (method ==
"PUT") {
40 method_idf = HTTP_METHOD_PUT;
41 }
else if (method ==
"DELETE") {
42 method_idf = HTTP_METHOD_DELETE;
43 }
else if (method ==
"PATCH") {
44 method_idf = HTTP_METHOD_PATCH;
47 ESP_LOGE(TAG,
"HTTP Request failed; Unsupported method");
51 bool secure = url.find(
"https:") != std::string::npos;
53 esp_http_client_config_t config = {};
55 config.url = url.c_str();
56 config.method = method_idf;
60 config.auth_type = HTTP_AUTH_TYPE_BASIC;
61 #if CONFIG_MBEDTLS_CERTIFICATE_BUNDLE 63 config.crt_bundle_attach = esp_crt_bundle_attach;
77 esp_http_client_handle_t client = esp_http_client_init(&config);
79 std::shared_ptr<HttpContainerIDF> container = std::make_shared<HttpContainerIDF>(client);
80 container->set_parent(
this);
82 container->set_secure(secure);
84 for (
const auto &header : headers) {
85 esp_http_client_set_header(client, header.name, header.value);
88 const int body_len = body.length();
90 esp_err_t err = esp_http_client_open(client, body_len);
93 ESP_LOGE(TAG,
"HTTP Request failed: %s", esp_err_to_name(err));
94 esp_http_client_cleanup(client);
99 int write_left = body_len;
101 const char *buf = body.c_str();
102 while (write_left > 0) {
103 int written = esp_http_client_write(client, buf + write_index, write_left);
108 write_left -= written;
109 write_index += written;
115 ESP_LOGE(TAG,
"HTTP Request failed: %s", esp_err_to_name(err));
116 esp_http_client_cleanup(client);
121 container->content_length = esp_http_client_fetch_headers(client);
123 container->status_code = esp_http_client_get_status_code(client);
132 while (
is_redirect(container->status_code) && num_redirects > 0) {
133 err = esp_http_client_set_redirection(client);
135 ESP_LOGE(TAG,
"esp_http_client_set_redirection failed: %s", esp_err_to_name(err));
137 esp_http_client_cleanup(client);
140 #if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE 141 char redirect_url[256]{};
142 if (esp_http_client_get_url(client, redirect_url,
sizeof(redirect_url) - 1) == ESP_OK) {
143 ESP_LOGV(TAG,
"redirecting to url: %s", redirect_url);
146 err = esp_http_client_open(client, 0);
148 ESP_LOGE(TAG,
"esp_http_client_open failed: %s", esp_err_to_name(err));
150 esp_http_client_cleanup(client);
155 container->content_length = esp_http_client_fetch_headers(client);
157 container->status_code = esp_http_client_get_status_code(client);
167 if (num_redirects == 0) {
172 ESP_LOGE(TAG,
"HTTP Request failed; URL: %s; Code: %d", url.c_str(), container->status_code);
181 int bufsize = std::min(max_len, this->content_length - this->bytes_read_);
189 int read_len = esp_http_client_read(this->client_, (
char *) buf, bufsize);
190 this->bytes_read_ += read_len;
200 esp_http_client_close(this->client_);
201 esp_http_client_cleanup(this->client_);
207 #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)
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