ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
http_request_idf.h
Go to the documentation of this file.
1#pragma once
2
3#include "http_request.h"
4
5#ifdef USE_ESP32
6
7#include <esp_event.h>
8#include <esp_idf_version.h>
9#include <esp_http_client.h>
10#include <esp_netif.h>
11#include <esp_tls.h>
12
13// The socket is needed to wait for the response without blocking; esp_http_client_get_socket() is new in 5.5.3
14#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 5, 3)
15#define USE_HTTP_REQUEST_IDF_SOCKET_POLL
16#endif
17
18namespace esphome::http_request {
19
20class HttpRequestIDF;
21
23 public:
24 HttpContainerIDF(esp_http_client_handle_t client) : client_(client) {}
25 int read(uint8_t *buf, size_t max_len) override;
26 int read_available(uint8_t *buf, size_t max_len) override;
27 void end() override;
28 bool is_read_complete() const override;
29 HttpRequestProgress poll() override;
30
32 void feed_wdt();
33
34 protected:
35 friend class HttpRequestIDF;
36 enum class Stage : uint8_t {
42 };
43
44 int read_(uint8_t *buf, size_t max_len);
46 bool socket_ready_(bool write);
48 HttpRequestProgress fail_(esp_err_t err);
52
53 esp_http_client_handle_t client_;
54 // Owned copy (not a reference): must outlive perform() for the response-header event handler
55 std::vector<std::string> collect_headers_;
56 // Request body for begin(), released once sent
57 std::string body_;
58 size_t body_sent_{0};
60 // Time of the last step forward, for the request timeout of begin()
63 uint16_t redirects_left_{0};
65 // An esp_http_client_open() of a non-blocking client returned ESP_ERR_HTTP_EAGAIN
66 bool connecting_{false};
67 bool async_{false};
68};
69
71 friend class HttpContainerIDF;
72
73 public:
74 // User provided, not "= default": `new(p) HttpRequestIDF()` would zero-fill .bss that is already zero.
76
77 void dump_config() override;
78
79 void set_buffer_size_rx(uint16_t buffer_size_rx) { this->buffer_size_rx_ = buffer_size_rx; }
80 void set_buffer_size_tx(uint16_t buffer_size_tx) { this->buffer_size_tx_ = buffer_size_tx; }
81 void set_verify_ssl(bool verify_ssl) { this->verify_ssl_ = verify_ssl; }
82 void set_ca_certificate(const char *ca_certificate) { this->ca_certificate_ = ca_certificate; }
83
84 std::shared_ptr<HttpContainer> begin(const char *url, const char *method, std::string &&body,
85 const std::vector<Header> &request_headers,
86 const std::vector<std::string> &lower_case_collect_headers,
87 uint32_t timeout_ms) override;
88
89 protected:
91 std::shared_ptr<HttpContainerIDF> start_container_(const char *url, const char *method, std::string &&body,
92 const std::vector<Header> &request_headers,
93 const std::vector<std::string> &lower_case_collect_headers,
94 uint32_t timeout_ms, bool async);
95 std::shared_ptr<HttpContainerIDF> create_container_(const char *url, const char *method,
96 const std::vector<Header> &request_headers,
97 const std::vector<std::string> &lower_case_collect_headers,
98 uint32_t timeout_ms, bool async);
99 std::shared_ptr<HttpContainer> perform(const char *url, const char *method, const std::string &body,
100 const std::vector<Header> &request_headers,
101 const std::vector<std::string> &lower_case_collect_headers,
102 uint32_t timeout_ms) override;
103 // if zero ESP-IDF will use DEFAULT_HTTP_BUF_SIZE
104 uint16_t buffer_size_rx_{};
105 uint16_t buffer_size_tx_{};
106 bool verify_ssl_{true};
107 const char *ca_certificate_{nullptr};
108
110 static esp_err_t http_event_handler(esp_http_client_event_t *evt);
111};
112
113} // namespace esphome::http_request
114
115#endif // USE_ESP32
int status_code
-1 indicates no response received yet
bool socket_ready_(bool write)
True when the socket can be read or written without waiting; always true when it cannot be checked.
HttpRequestProgress response_failed_(int status_code)
Finish with an unsuccessful status, which is reported through on_response like a success.
HttpRequestProgress poll() override
int read(uint8_t *buf, size_t max_len) override
HttpContainerIDF(esp_http_client_handle_t client)
int read_(uint8_t *buf, size_t max_len)
std::vector< std::string > collect_headers_
HttpRequestProgress fail_(esp_err_t err)
void feed_wdt()
Feeds the watchdog timer if the executing task has one attached.
int read_available(uint8_t *buf, size_t max_len) override
void set_buffer_size_tx(uint16_t buffer_size_tx)
std::shared_ptr< HttpContainerIDF > start_container_(const char *url, const char *method, std::string &&body, const std::vector< Header > &request_headers, const std::vector< std::string > &lower_case_collect_headers, uint32_t timeout_ms, bool async)
A container that poll() takes from the connect through to the response headers.
std::shared_ptr< HttpContainer > perform(const char *url, const char *method, const std::string &body, const std::vector< Header > &request_headers, const std::vector< std::string > &lower_case_collect_headers, uint32_t timeout_ms) override
void set_buffer_size_rx(uint16_t buffer_size_rx)
std::shared_ptr< HttpContainer > begin(const char *url, const char *method, std::string &&body, const std::vector< Header > &request_headers, const std::vector< std::string > &lower_case_collect_headers, uint32_t timeout_ms) override
std::shared_ptr< HttpContainerIDF > create_container_(const char *url, const char *method, const std::vector< Header > &request_headers, const std::vector< std::string > &lower_case_collect_headers, uint32_t timeout_ms, bool async)
static esp_err_t http_event_handler(esp_http_client_event_t *evt)
Monitors the http client events to gather response headers.
void set_ca_certificate(const char *ca_certificate)
HttpRequestProgress
Progress of a request started with HttpRequestComponent::begin()
static void uint32_t