84 const std::string &lower_header_name) {
85 for (
const auto &
h : lower_case_collect_headers) {
86 if (
h == lower_header_name)
158static constexpr int HTTP_ERROR_CONNECTION_CLOSED = -1;
189 bool is_read_complete) {
190 if (bytes_read_or_error > 0) {
191 last_data_time =
millis();
194 if (bytes_read_or_error < 0) {
198 if (is_read_complete) {
201 if (
millis() - last_data_time >= timeout_ms) {
208class HttpRequestComponent;
244 virtual int read(uint8_t *buf,
size_t max_len) = 0;
288 uint32_t timeout_ms) {
289 size_t read_index = 0;
290 uint32_t last_data_time =
millis();
292 while (read_index < total_size) {
293 int read_bytes_or_error = container->
read(buffer + read_index, std::min(chunk_size, total_size - read_index));
308 read_index += read_bytes_or_error;
315 void process(
const std::shared_ptr<HttpContainer> &container, std::string &response_body) {
316 this->
trigger(container, response_body);
333 std::shared_ptr<HttpContainer>
get(
const std::string &url) {
334 return this->
start(url,
"GET",
"", std::vector<Header>{});
336 std::shared_ptr<HttpContainer>
get(
const std::string &url,
const std::vector<Header> &request_headers) {
337 return this->
start(url,
"GET",
"", request_headers);
339 std::shared_ptr<HttpContainer>
get(
const std::string &url,
const std::vector<Header> &request_headers,
340 const std::vector<std::string> &lower_case_collect_headers) {
341 return this->
start(url,
"GET",
"", request_headers, lower_case_collect_headers);
343 std::shared_ptr<HttpContainer>
post(
const std::string &url,
const std::string &body) {
344 return this->
start(url,
"POST", body, std::vector<Header>{});
346 std::shared_ptr<HttpContainer>
post(
const std::string &url,
const std::string &body,
347 const std::vector<Header> &request_headers) {
348 return this->
start(url,
"POST", body, request_headers);
350 std::shared_ptr<HttpContainer>
post(
const std::string &url,
const std::string &body,
351 const std::vector<Header> &request_headers,
352 const std::vector<std::string> &lower_case_collect_headers) {
353 return this->
start(url,
"POST", body, request_headers, lower_case_collect_headers);
357 ESPDEPRECATED(
"Pass request_headers as std::vector<Header> instead of std::list. Removed in 2027.1.0.",
"2026.7.0")
359 return this->
get(url, std::vector<Header>(request_headers.begin(), request_headers.end()));
362 ESPDEPRECATED(
"Pass request_headers as std::vector<Header> instead of std::list. Removed in 2027.1.0.",
"2026.7.0")
364 const std::vector<std::
string> &collect_headers) {
365 return this->
get(url, std::vector<Header>(request_headers.begin(), request_headers.end()), collect_headers);
368 ESPDEPRECATED(
"Pass request_headers as std::vector<Header> instead of std::list. Removed in 2027.1.0.",
"2026.7.0")
369 std::shared_ptr<
HttpContainer>
post(const std::
string &url, const std::
string &body,
370 const std::list<
Header> &request_headers) {
371 return this->
post(url, body, std::vector<Header>(request_headers.begin(), request_headers.end()));
374 ESPDEPRECATED(
"Pass request_headers as std::vector<Header> instead of std::list. Removed in 2027.1.0.",
"2026.7.0")
375 std::shared_ptr<
HttpContainer>
post(const std::
string &url, const std::
string &body,
376 const std::list<
Header> &request_headers,
377 const std::vector<std::
string> &collect_headers) {
378 return this->
post(url, body, std::vector<Header>(request_headers.begin(), request_headers.end()), collect_headers);
381 std::shared_ptr<HttpContainer>
start(
const std::string &url,
const std::string &method,
const std::string &body,
382 const std::vector<Header> &request_headers) {
384 return this->
perform(url, method, body, request_headers, {});
388 ESPDEPRECATED(
"Pass request_headers as std::vector<Header> instead of std::list. Removed in 2027.1.0.",
"2026.7.0")
389 std::shared_ptr<
HttpContainer>
start(const std::
string &url, const std::
string &method, const std::
string &body,
390 const std::list<
Header> &request_headers) {
391 return this->
start(url, method, body, std::vector<Header>(request_headers.begin(), request_headers.end()));
395 ESPDEPRECATED(
"Pass collect_headers as std::vector<std::string> instead of std::set. Removed in 2027.1.0.",
397 std::shared_ptr<
HttpContainer>
start(const std::
string &url, const std::
string &method, const std::
string &body,
398 const std::vector<
Header> &request_headers,
399 const std::set<std::
string> &collect_headers) {
400 std::vector<std::string> lower;
401 lower.reserve(collect_headers.size());
402 for (
const auto &
h : collect_headers) {
405 return this->
perform(url, method, body, request_headers, lower);
409 ESPDEPRECATED(
"Pass request_headers as std::vector<Header> instead of std::list, and collect_headers as "
410 "std::vector<std::string> instead of std::set. Removed in 2027.1.0.",
412 std::shared_ptr<
HttpContainer>
start(const std::
string &url, const std::
string &method, const std::
string &body,
413 const std::list<
Header> &request_headers,
414 const std::set<std::
string> &collect_headers) {
415 std::vector<std::string> lower;
416 lower.reserve(collect_headers.size());
417 for (
const auto &
h : collect_headers) {
420 return this->
perform(url, method, body, std::vector<Header>(request_headers.begin(), request_headers.end()), lower);
424 ESPDEPRECATED(
"Pass request_headers as std::vector<Header> instead of std::list. Removed in 2027.1.0.",
"2026.7.0")
425 std::shared_ptr<
HttpContainer>
start(const std::
string &url, const std::
string &method, const std::
string &body,
426 const std::list<
Header> &request_headers,
427 const std::vector<std::
string> &lower_case_collect_headers) {
428 return this->
perform(url, method, body, std::vector<Header>(request_headers.begin(), request_headers.end()),
429 lower_case_collect_headers);
432 std::shared_ptr<HttpContainer>
start(
const std::string &url,
const std::string &method,
const std::string &body,
433 const std::vector<Header> &request_headers,
434 const std::vector<std::string> &lower_case_collect_headers) {
435 return this->
perform(url, method, body, request_headers, lower_case_collect_headers);
439 virtual std::shared_ptr<HttpContainer>
perform(
const std::string &url,
const std::string &method,
440 const std::string &body,
const std::vector<Header> &request_headers,
441 const std::vector<std::string> &lower_case_collect_headers) = 0;
455#ifdef USE_HTTP_REQUEST_RESPONSE
471#ifdef USE_HTTP_REQUEST_RESPONSE
484 void play(
const Ts &...
x)
override {
486 if (this->body_.has_value()) {
487 body = this->body_.value(
x...);
489 if (!this->
json_.empty()) {
497 std::vector<Header> request_headers;
500 request_headers.push_back({key,
val.value(
x...)});
503 auto container = this->
parent_->
start(this->url_.value(
x...), this->method_.value(
x...), body, request_headers,
504 this->lower_case_collect_headers_);
506 auto captured_args = std::make_tuple(
x...);
508 if (container ==
nullptr) {
509 std::apply([
this](Ts... captured_args_inner) { this->error_trigger_.trigger(captured_args_inner...); },
515#ifdef USE_HTTP_REQUEST_RESPONSE
516 if (this->capture_response_.value(
x...)) {
517 std::string response_body;
519 uint8_t *buf = allocator.
allocate(max_length);
520 if (buf !=
nullptr) {
523 size_t read_index = 0;
524 uint32_t last_data_time =
millis();
526 while (container->get_bytes_read() < max_length) {
527 int read_or_error = container->read(buf + read_index, std::min<size_t>(max_length - read_index, 512));
536 read_index += read_or_error;
538 response_body.reserve(read_index);
539 response_body.assign((
char *) buf, read_index);
543 [
this, &container, &response_body](Ts... captured_args_inner) {
544 this->success_trigger_with_response_.trigger(container, response_body, captured_args_inner...);
550 std::apply([
this, &container](
551 Ts... captured_args_inner) { this->success_trigger_.trigger(container, captured_args_inner...); },
559 for (
const auto &item : this->
json_) {
560 auto val = item.second;
561 root[item.first] =
val.value(
x...);
570#ifdef USE_HTTP_REQUEST_RESPONSE
void feed_wdt(uint32_t time=0)
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
Helper class to easily give an object a parent of type T.
An STL allocator that uses SPI or internal RAM.
void deallocate(T *p, size_t n)
void trigger(const Ts &...x)
std::string get_response_header(const std::string &header_name)
virtual int read(uint8_t *buf, size_t max_len)=0
Read data from the HTTP response body.
virtual bool is_read_complete() const
Check if all expected content has been read.
std::vector< Header > response_headers_
virtual ~HttpContainer()=default
int status_code
-1 indicates no response received yet
void set_chunked(bool chunked)
size_t get_bytes_read() const
void set_secure(bool secure)
bool is_chunked_
True if response uses chunked transfer encoding.
float get_setup_priority() const override
ESPDEPRECATED("Pass request_headers as std::vector<Header> instead of std::list. Removed in 2027.1.0.", "2026.7.0") std
uint32_t get_watchdog_timeout() const
std::shared_ptr< HttpContainer > post(const std::string &url, const std::string &body)
void set_useragent(const char *useragent)
ESPDEPRECATED("Pass request_headers as std::vector<Header> instead of std::list, and collect_headers as " "std::vector<std::string> instead of std::set. Removed in 2027.1.0.", "2026.7.0") std
std::shared_ptr< HttpContainer > start(const std::string &url, const std::string &method, const std::string &body, const std::vector< Header > &request_headers)
std::shared_ptr< HttpContainer > post(const std::string &url, const std::string &body, const std::vector< Header > &request_headers, const std::vector< std::string > &lower_case_collect_headers)
uint32_t watchdog_timeout_
void set_follow_redirects(bool follow_redirects)
uint32_t get_timeout() const
std::shared_ptr< HttpContainer > get(const std::string &url, const std::vector< Header > &request_headers, const std::vector< std::string > &lower_case_collect_headers)
void set_redirect_limit(uint16_t limit)
void set_watchdog_timeout(uint32_t watchdog_timeout)
ESPDEPRECATED("Pass collect_headers as std::vector<std::string> instead of std::set. Removed in 2027.1.0.", "2026.7.0") std
std::shared_ptr< HttpContainer > post(const std::string &url, const std::string &body, const std::vector< Header > &request_headers)
void set_timeout(uint32_t timeout)
std::shared_ptr< HttpContainer > start(const std::string &url, const std::string &method, const std::string &body, const std::vector< Header > &request_headers, const std::vector< std::string > &lower_case_collect_headers)
std::shared_ptr< HttpContainer > get(const std::string &url)
virtual std::shared_ptr< HttpContainer > perform(const std::string &url, const std::string &method, const std::string &body, const std::vector< Header > &request_headers, const std::vector< std::string > &lower_case_collect_headers)=0
std::shared_ptr< HttpContainer > get(const std::string &url, const std::vector< Header > &request_headers)
void dump_config() override
void process(const std::shared_ptr< HttpContainer > &container, std::string &response_body)
void set_max_response_buffer_size(size_t max_response_buffer_size)
void play(const Ts &...x) override
HttpRequestComponent * parent_
TEMPLATABLE_VALUE(std::string, url) TEMPLATABLE_VALUE(const char *
Trigger< std::shared_ptr< HttpContainer >, std::string &, Ts... > success_trigger_with_response_
void add_collect_header(const char *value)
Trigger< Ts... > error_trigger_
method capture_response void init_request_headers(size_t count)
Trigger< Ts... > * get_error_trigger()
FixedVector< std::pair< const char *, TemplatableValue< const char *, Ts... > > > request_headers_
std::function< void(Ts..., JsonObject)> json_func_
Trigger< std::shared_ptr< HttpContainer >, Ts... > success_trigger_
std::vector< std::string > lower_case_collect_headers_
void encode_json_func_(Ts... x, JsonObject root)
void set_json(std::function< void(Ts..., JsonObject)> json_func)
void add_json(const char *key, TemplatableValue< std::string, Ts... > value)
Trigger< std::shared_ptr< HttpContainer >, std::string &, Ts... > * get_success_trigger_with_response()
void add_request_header(const char *key, TemplatableValue< const char *, Ts... > value)
void encode_json_(Ts... x, JsonObject root)
size_t max_response_buffer_size_
Trigger< std::shared_ptr< HttpContainer >, Ts... > * get_success_trigger()
HttpRequestSendAction(HttpRequestComponent *parent)
FixedVector< std::pair< const char *, TemplatableValue< std::string, Ts... > > > json_
void init_json(size_t count)
bool should_collect_header(const std::vector< std::string > &lower_case_collect_headers, const std::string &lower_header_name)
Check if a header name should be collected (linear scan, fine for small lists)
@ HTTP_STATUS_PARTIAL_CONTENT
@ HTTP_STATUS_LENGTH_REQUIRED
@ HTTP_STATUS_PERMANENT_REDIRECT
@ HTTP_STATUS_RESET_CONTENT
@ HTTP_STATUS_METHOD_NOT_ALLOWED
@ HTTP_STATUS_NOT_MODIFIED
@ HTTP_STATUS_INTERNAL_ERROR
@ HTTP_STATUS_UNAUTHORIZED
@ HTTP_STATUS_MULTIPLE_CHOICES
@ HTTP_STATUS_NOT_ACCEPTABLE
@ HTTP_STATUS_TEMPORARY_REDIRECT
@ HTTP_STATUS_BAD_REQUEST
@ HTTP_STATUS_MOVED_PERMANENTLY
HttpReadLoopResult
Result of processing a non-blocking read with timeout (for manual loops)
@ TIMEOUT
Timeout waiting for data, caller should exit loop.
@ COMPLETE
All content has been read, caller should exit loop.
@ ERROR
Read error, caller should exit loop.
@ RETRY
No data yet, already delayed, caller should continue loop.
@ DATA
Data was read, process it.
bool is_success(int const status)
Checks if the given HTTP status code indicates a successful request.
HttpReadLoopResult http_read_loop_result(int bytes_read_or_error, uint32_t &last_data_time, uint32_t timeout_ms, bool is_read_complete)
Process a read result with timeout tracking and delay handling.
bool is_redirect(int const status)
Returns true if the HTTP status code is a redirect.
HttpReadResult http_read_fully(HttpContainer *container, uint8_t *buffer, size_t total_size, size_t chunk_size, uint32_t timeout_ms)
Read data from HTTP container into buffer with timeout handling Handles feed_wdt, yield,...
HttpReadStatus
Status of a read operation.
@ TIMEOUT
Timeout waiting for data.
@ ERROR
Read error occurred.
@ OK
Read completed successfully.
SerializationBuffer build_json(const json_build_t &f)
Build a JSON string with the provided json build function.
constexpr float AFTER_WIFI
For components that should be initialized after WiFi is connected.
std::string str_lower_case(const std::string &str)
Convert the string to lower case.
void HOT delay(uint32_t ms)
uint32_t IRAM_ATTR HOT millis()
Application App
Global storage of Application pointer - only one Application can exist.
Result of an HTTP read operation.
HttpReadStatus status
Status of the read operation.
int error_code
Error code from read() on failure, 0 on success.