ESPHome 2026.10.2
Loading...
Searching...
No Matches
http_request.h
Go to the documentation of this file.
1#pragma once
2
3#include <list>
4#include <memory>
5#include <set>
6#include <utility>
7#include <vector>
8
16#include "esphome/core/log.h"
17
18namespace esphome::http_request {
19
20struct Header {
21 std::string name;
22 std::string value;
23};
24
25// Some common HTTP status codes
53
60inline bool is_redirect(int const status) {
61 switch (status) {
67 return true;
68 default:
69 return false;
70 }
71}
72
81inline bool is_success(int const status) { return status >= HTTP_STATUS_OK && status < HTTP_STATUS_MULTIPLE_CHOICES; }
82
84inline bool should_collect_header(const std::vector<std::string> &lower_case_collect_headers,
85 const std::string &lower_header_name) {
86 for (const auto &h : lower_case_collect_headers) {
87 if (h == lower_header_name)
88 return true;
89 }
90 return false;
91}
92
93/*
94 * HTTP Container Read Semantics
95 * =============================
96 *
97 * IMPORTANT: These semantics differ from standard BSD sockets!
98 *
99 * BSD socket read() returns:
100 * > 0: bytes read
101 * == 0: connection closed (EOF)
102 * < 0: error (check errno)
103 *
104 * HttpContainer::read() returns:
105 * > 0: bytes read successfully
106 * == 0: no data available yet OR all content read
107 * (caller should check bytes_read vs content_length)
108 * < 0: error or connection closed (caller should EXIT)
109 * HTTP_ERROR_CONNECTION_CLOSED (-1) = connection closed prematurely
110 * other negative values = platform-specific errors
111 *
112 * Platform behaviors:
113 * - ESP-IDF: blocking reads, 0 only returned when all content read
114 * - Arduino: non-blocking, 0 means "no data yet" or "all content read"
115 *
116 * Chunked responses that complete in a reasonable time work correctly on both
117 * platforms. The limitation below applies only to *streaming* chunked
118 * responses where data arrives slowly over a long period.
119 *
120 * Streaming chunked responses are NOT supported (all platforms):
121 * The read helpers (http_read_loop_result, http_read_fully) block the main
122 * event loop until all response data is received. For streaming responses
123 * where data trickles in slowly (e.g., TTS streaming via ffmpeg proxy),
124 * this starves the event loop on both ESP-IDF and Arduino. If data arrives
125 * just often enough to avoid the caller's timeout, the loop runs
126 * indefinitely. If data stops entirely, ESP-IDF fails with
127 * -ESP_ERR_HTTP_EAGAIN (transport timeout) while Arduino spins with
128 * delay(1) until the caller's timeout fires. Supporting streaming requires
129 * a non-blocking incremental read pattern that yields back to the event
130 * loop between chunks. Components that need streaming should use
131 * esp_http_client directly on a separate FreeRTOS task with
132 * esp_http_client_is_complete_data_received() for completion detection
133 * (see audio_reader.cpp for an example).
134 *
135 * Chunked transfer encoding - platform differences:
136 * - ESP-IDF HttpContainer:
137 * HttpContainerIDF overrides is_read_complete() to call
138 * esp_http_client_is_complete_data_received(), which is the
139 * authoritative completion check for both chunked and non-chunked
140 * transfers. When esp_http_client_read() returns 0 for a completed
141 * chunked response, read() returns 0 and is_read_complete() returns
142 * true, so callers get COMPLETE from http_read_loop_result().
143 *
144 * - Arduino HttpContainer:
145 * Chunked responses are decoded internally (see
146 * HttpContainerArduino::read_chunked_()). When the final chunk arrives,
147 * is_chunked_ is cleared and content_length is set to bytes_read_.
148 * Completion is then detected via is_read_complete(), and a subsequent
149 * read() returns 0 to indicate "all content read" (not
150 * HTTP_ERROR_CONNECTION_CLOSED).
151 *
152 * Use the helper functions below instead of checking return values directly:
153 * - http_read_loop_result(): for manual loops with per-chunk processing
154 * - http_read_fully(): for simple "read N bytes into buffer" operations
155 */
156
159static constexpr int HTTP_ERROR_CONNECTION_CLOSED = -1;
160
162enum class HttpReadStatus : uint8_t {
163 OK,
164 ERROR,
165 TIMEOUT,
166};
167
173
175enum class HttpReadLoopResult : uint8_t {
176 DATA,
177 COMPLETE,
178 RETRY,
179 ERROR,
180 TIMEOUT,
181};
182
189inline HttpReadLoopResult http_read_loop_result(int bytes_read_or_error, uint32_t &last_data_time, uint32_t timeout_ms,
190 bool is_read_complete) {
191 if (bytes_read_or_error > 0) {
192 last_data_time = millis();
194 }
195 if (bytes_read_or_error < 0) {
197 }
198 // bytes_read_or_error == 0: either "no data yet" or "all content read"
199 if (is_read_complete) {
201 }
202 if (millis() - last_data_time >= timeout_ms) {
204 }
205 delay(1); // Small delay to prevent tight spinning
207}
208
209class HttpRequestComponent;
210
211class HttpContainer : public Parented<HttpRequestComponent> {
212 public:
213 virtual ~HttpContainer() = default;
214 size_t content_length{0};
215 int status_code{-1};
217
245 virtual int read(uint8_t *buf, size_t max_len) = 0;
246 virtual void end() = 0;
247
248 void set_secure(bool secure) { this->secure_ = secure; }
249 void set_chunked(bool chunked) { this->is_chunked_ = chunked; }
250
251 size_t get_bytes_read() const { return this->bytes_read_; }
252
259 virtual bool is_read_complete() const {
260 // Per RFC 9112, these responses have no body:
261 // - 1xx (Informational), 204 No Content, 205 Reset Content, 304 Not Modified
262 if ((this->status_code >= 100 && this->status_code < 200) || this->status_code == HTTP_STATUS_NO_CONTENT ||
264 return true;
265 }
266 // For non-chunked responses, complete when bytes_read >= content_length
267 // This handles both Content-Length: 0 and Content-Length: N cases
268 return !this->is_chunked_ && this->bytes_read_ >= this->content_length;
269 }
270
276 std::string get_response_header(const std::string &header_name);
277
278 protected:
279 size_t bytes_read_{0};
280 bool secure_{false};
281 bool is_chunked_{false};
282 std::vector<Header> response_headers_{};
283};
284
293inline HttpReadResult http_read_fully(HttpContainer *container, uint8_t *buffer, size_t total_size, size_t chunk_size,
294 uint32_t timeout_ms) {
295 size_t read_index = 0;
296 uint32_t last_data_time = millis();
297
298 while (read_index < total_size) {
299 int read_bytes_or_error = container->read(buffer + read_index, std::min(chunk_size, total_size - read_index));
300
301 App.feed_wdt();
302 yield();
303
304 auto result = http_read_loop_result(read_bytes_or_error, last_data_time, timeout_ms, container->is_read_complete());
305 if (result == HttpReadLoopResult::RETRY)
306 continue;
307 if (result == HttpReadLoopResult::COMPLETE)
308 break; // Server sent less data than requested, but transfer is complete
309 if (result == HttpReadLoopResult::ERROR)
310 return {HttpReadStatus::ERROR, read_bytes_or_error};
311 if (result == HttpReadLoopResult::TIMEOUT)
312 return {HttpReadStatus::TIMEOUT, 0};
313
314 read_index += read_bytes_or_error;
315 }
316 return {HttpReadStatus::OK, 0};
317}
318
319class HttpRequestResponseTrigger final : public Trigger<std::shared_ptr<HttpContainer>, std::string &> {
320 public:
321 void process(const std::shared_ptr<HttpContainer> &container, std::string &response_body) {
322 this->trigger(container, response_body);
323 }
324};
325
327 public:
328 void dump_config() override;
329 float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
330
331 void set_useragent(const char *useragent) { this->useragent_ = useragent; }
332 void set_timeout(uint32_t timeout) { this->timeout_ = timeout; }
333 uint32_t get_timeout() const { return this->timeout_; }
334 void set_watchdog_timeout(uint32_t watchdog_timeout) { this->watchdog_timeout_ = watchdog_timeout; }
336 void set_follow_redirects(bool follow_redirects) { this->follow_redirects_ = follow_redirects; }
337 void set_redirect_limit(uint16_t limit) { this->redirect_limit_ = limit; }
338
339 std::shared_ptr<HttpContainer> get(const char *url) { return this->start(url, "GET", "", std::vector<Header>{}); }
340 std::shared_ptr<HttpContainer> get(const char *url, const std::vector<Header> &request_headers) {
341 return this->start(url, "GET", "", request_headers);
342 }
343 std::shared_ptr<HttpContainer> get(const char *url, const std::vector<Header> &request_headers,
344 const std::vector<std::string> &lower_case_collect_headers) {
345 return this->start(url, "GET", "", request_headers, lower_case_collect_headers);
346 }
347 std::shared_ptr<HttpContainer> post(const char *url, const std::string &body) {
348 return this->start(url, "POST", body, std::vector<Header>{});
349 }
350 std::shared_ptr<HttpContainer> post(const char *url, const std::string &body,
351 const std::vector<Header> &request_headers) {
352 return this->start(url, "POST", body, request_headers);
353 }
354 std::shared_ptr<HttpContainer> post(const char *url, const std::string &body,
355 const std::vector<Header> &request_headers,
356 const std::vector<std::string> &lower_case_collect_headers) {
357 return this->start(url, "POST", body, request_headers, lower_case_collect_headers);
358 }
359
360 std::shared_ptr<HttpContainer> get(const std::string &url) { return this->get(url.c_str()); }
361 std::shared_ptr<HttpContainer> get(const std::string &url, const std::vector<Header> &request_headers) {
362 return this->get(url.c_str(), request_headers);
363 }
364 std::shared_ptr<HttpContainer> get(const std::string &url, const std::vector<Header> &request_headers,
365 const std::vector<std::string> &lower_case_collect_headers) {
366 return this->get(url.c_str(), request_headers, lower_case_collect_headers);
367 }
368 std::shared_ptr<HttpContainer> post(const std::string &url, const std::string &body) {
369 return this->post(url.c_str(), body);
370 }
371 std::shared_ptr<HttpContainer> post(const std::string &url, const std::string &body,
372 const std::vector<Header> &request_headers) {
373 return this->post(url.c_str(), body, request_headers);
374 }
375 std::shared_ptr<HttpContainer> post(const std::string &url, const std::string &body,
376 const std::vector<Header> &request_headers,
377 const std::vector<std::string> &lower_case_collect_headers) {
378 return this->post(url.c_str(), body, request_headers, lower_case_collect_headers);
379 }
380
381 // Remove before 2027.1.0
382 ESPDEPRECATED("Pass request_headers as std::vector<Header> instead of std::list. Removed in 2027.1.0.", "2026.7.0")
383 std::shared_ptr<HttpContainer> get(const std::string &url, const std::list<Header> &request_headers) {
384 return this->get(url, std::vector<Header>(request_headers.begin(), request_headers.end()));
385 }
386 // Remove before 2027.1.0
387 ESPDEPRECATED("Pass request_headers as std::vector<Header> instead of std::list. Removed in 2027.1.0.", "2026.7.0")
388 std::shared_ptr<HttpContainer> get(const std::string &url, const std::list<Header> &request_headers,
389 const std::vector<std::string> &collect_headers) {
390 return this->get(url, std::vector<Header>(request_headers.begin(), request_headers.end()), collect_headers);
391 }
392 // Remove before 2027.1.0
393 ESPDEPRECATED("Pass request_headers as std::vector<Header> instead of std::list. Removed in 2027.1.0.", "2026.7.0")
394 std::shared_ptr<HttpContainer> post(const std::string &url, const std::string &body,
395 const std::list<Header> &request_headers) {
396 return this->post(url, body, std::vector<Header>(request_headers.begin(), request_headers.end()));
397 }
398 // Remove before 2027.1.0
399 ESPDEPRECATED("Pass request_headers as std::vector<Header> instead of std::list. Removed in 2027.1.0.", "2026.7.0")
400 std::shared_ptr<HttpContainer> post(const std::string &url, const std::string &body,
401 const std::list<Header> &request_headers,
402 const std::vector<std::string> &collect_headers) {
403 return this->post(url, body, std::vector<Header>(request_headers.begin(), request_headers.end()), collect_headers);
404 }
405
406 std::shared_ptr<HttpContainer> start(const char *url, const char *method, const std::string &body,
407 const std::vector<Header> &request_headers) {
408 // Call perform() directly to avoid ambiguity with the deprecated overloads
409 return this->perform(url, method, body, request_headers, {});
410 }
411 std::shared_ptr<HttpContainer> start(const std::string &url, const std::string &method, const std::string &body,
412 const std::vector<Header> &request_headers) {
413 return this->start(url.c_str(), method.c_str(), body, request_headers);
414 }
415
416 // Remove before 2027.1.0
417 ESPDEPRECATED("Pass request_headers as std::vector<Header> instead of std::list. Removed in 2027.1.0.", "2026.7.0")
418 std::shared_ptr<HttpContainer> start(const std::string &url, const std::string &method, const std::string &body,
419 const std::list<Header> &request_headers) {
420 return this->start(url, method, body, std::vector<Header>(request_headers.begin(), request_headers.end()));
421 }
422
423 // Remove before 2027.1.0
424 ESPDEPRECATED("Pass collect_headers as std::vector<std::string> instead of std::set. Removed in 2027.1.0.",
425 "2026.7.0")
426 std::shared_ptr<HttpContainer> start(const std::string &url, const std::string &method, const std::string &body,
427 const std::vector<Header> &request_headers,
428 const std::set<std::string> &collect_headers) {
429 std::vector<std::string> lower;
430 lower.reserve(collect_headers.size());
431 for (const auto &h : collect_headers) {
432 lower.push_back(str_lower_case(h)); // NOLINT
433 }
434 return this->perform(url.c_str(), method.c_str(), body, request_headers, lower);
435 }
436
437 // Remove before 2027.1.0
438 ESPDEPRECATED("Pass request_headers as std::vector<Header> instead of std::list, and collect_headers as "
439 "std::vector<std::string> instead of std::set. Removed in 2027.1.0.",
440 "2026.7.0")
441 std::shared_ptr<HttpContainer> start(const std::string &url, const std::string &method, const std::string &body,
442 const std::list<Header> &request_headers,
443 const std::set<std::string> &collect_headers) {
444 std::vector<std::string> lower;
445 lower.reserve(collect_headers.size());
446 for (const auto &h : collect_headers) {
447 lower.push_back(str_lower_case(h)); // NOLINT
448 }
449 return this->perform(url.c_str(), method.c_str(), body,
450 std::vector<Header>(request_headers.begin(), request_headers.end()), lower);
451 }
452
453 // Remove before 2027.1.0
454 ESPDEPRECATED("Pass request_headers as std::vector<Header> instead of std::list. Removed in 2027.1.0.", "2026.7.0")
455 std::shared_ptr<HttpContainer> start(const std::string &url, const std::string &method, const std::string &body,
456 const std::list<Header> &request_headers,
457 const std::vector<std::string> &lower_case_collect_headers) {
458 return this->perform(url.c_str(), method.c_str(), body,
459 std::vector<Header>(request_headers.begin(), request_headers.end()),
460 lower_case_collect_headers);
461 }
462
463 std::shared_ptr<HttpContainer> start(const char *url, const char *method, const std::string &body,
464 const std::vector<Header> &request_headers,
465 const std::vector<std::string> &lower_case_collect_headers) {
466 return this->perform(url, method, body, request_headers, lower_case_collect_headers);
467 }
468 std::shared_ptr<HttpContainer> start(const std::string &url, const std::string &method, const std::string &body,
469 const std::vector<Header> &request_headers,
470 const std::vector<std::string> &lower_case_collect_headers) {
471 return this->start(url.c_str(), method.c_str(), body, request_headers, lower_case_collect_headers);
472 }
473
474 protected:
475 virtual std::shared_ptr<HttpContainer> perform(const char *url, const char *method, const std::string &body,
476 const std::vector<Header> &request_headers,
477 const std::vector<std::string> &lower_case_collect_headers) = 0;
478 const char *useragent_{nullptr};
480 uint16_t redirect_limit_{};
483};
484
485template<typename... Ts> class HttpRequestSendAction final : public Action<Ts...> {
486 public:
488 TEMPLATABLE_VALUE(std::string, url)
489 TEMPLATABLE_VALUE(const char *, method)
490 TEMPLATABLE_VALUE(std::string, body)
491#ifdef USE_HTTP_REQUEST_RESPONSE
492 TEMPLATABLE_VALUE(bool, capture_response)
493#endif
494
495 void init_request_headers(size_t count) { this->request_headers_.init(count); }
497 this->request_headers_.push_back({key, value});
498 }
499
500 void add_collect_header(const char *value) { this->lower_case_collect_headers_.emplace_back(value); }
501
502 void init_json(size_t count) { this->json_.init(count); }
503 void add_json(const char *key, TemplatableValue<std::string, Ts...> value) { this->json_.push_back({key, value}); }
504
505 void set_json(std::function<void(Ts..., JsonObject)> json_func) { this->json_func_ = json_func; }
506
507#ifdef USE_HTTP_REQUEST_RESPONSE
511#endif
513
514 Trigger<Ts...> *get_error_trigger() { return &this->error_trigger_; }
515
516 void set_max_response_buffer_size(size_t max_response_buffer_size) {
517 this->max_response_buffer_size_ = max_response_buffer_size;
518 }
519
520 void play(const Ts &...x) override {
521 std::string body;
522 if (this->body_.has_value()) {
523 body = this->body_.value(x...);
524 }
525 if (!this->json_.empty()) {
526 body = json::build_json([this, x...](JsonObject root) mutable { this->encode_json_(x..., root); });
527 }
528 if (this->json_func_ != nullptr) {
529 body = json::build_json([this, x...](JsonObject root) mutable { this->json_func_(x..., root); });
530 }
531 std::vector<Header> request_headers;
532 request_headers.reserve(this->request_headers_.size());
533 for (const auto &[key, val] : this->request_headers_) {
534 request_headers.push_back({key, val.value(x...)});
535 }
536
537 auto container = this->parent_->start(this->url_.value(x...).c_str(), this->method_.value(x...), body,
538 request_headers, this->lower_case_collect_headers_);
539
540 auto captured_args = std::make_tuple(x...);
541
542 if (container == nullptr) {
543 std::apply([this](Ts... captured_args_inner) { this->error_trigger_.trigger(captured_args_inner...); },
544 captured_args);
545 return;
546 }
547
548#ifdef USE_HTTP_REQUEST_RESPONSE
549 if (this->capture_response_.value(x...)) {
550 size_t max_length = this->max_response_buffer_size_;
551 std::string response_body;
552 RAMAllocator<uint8_t> allocator;
553 uint8_t *buf = allocator.allocate(max_length);
554 if (buf != nullptr) {
555 // NOTE: HttpContainer::read() has non-BSD socket semantics - see top of this file
556 // Use http_read_loop_result() helper instead of checking return values directly
557 size_t read_index = 0;
558 uint32_t last_data_time = millis();
559 const uint32_t read_timeout = this->parent_->get_timeout();
560 while (container->get_bytes_read() < max_length) {
561 int read_or_error = container->read(buf + read_index, std::min<size_t>(max_length - read_index, 512));
562 App.feed_wdt();
563 yield();
564 auto result =
565 http_read_loop_result(read_or_error, last_data_time, read_timeout, container->is_read_complete());
566 if (result == HttpReadLoopResult::RETRY)
567 continue;
568 if (result != HttpReadLoopResult::DATA)
569 break; // COMPLETE, ERROR, or TIMEOUT
570 read_index += read_or_error;
571 }
572 response_body.reserve(read_index);
573 response_body.assign((char *) buf, read_index);
574 allocator.deallocate(buf, max_length);
575 }
576 std::apply(
577 [this, &container, &response_body](Ts... captured_args_inner) {
578 this->success_trigger_with_response_.trigger(container, response_body, captured_args_inner...);
579 },
580 captured_args);
581 } else
582#endif
583 {
584 std::apply([this, &container](
585 Ts... captured_args_inner) { this->success_trigger_.trigger(container, captured_args_inner...); },
586 captured_args);
587 }
588 container->end();
589 }
590
591 protected:
592 void encode_json_(Ts... x, JsonObject root) {
593 for (const auto &item : this->json_) {
594 auto val = item.second;
595 root[item.first] = val.value(x...);
596 }
597 }
599 FixedVector<std::pair<const char *, TemplatableFn<const char *, Ts...>>> request_headers_{};
600 std::vector<std::string> lower_case_collect_headers_{"content-type", "content-length"};
601 FixedVector<std::pair<const char *, TemplatableValue<std::string, Ts...>>> json_{};
602 std::function<void(Ts..., JsonObject)> json_func_{nullptr};
603#ifdef USE_HTTP_REQUEST_RESPONSE
605#endif
608
609 size_t max_response_buffer_size_{SIZE_MAX};
610};
611
612} // namespace esphome::http_request
Heap-allocating helper functions.
uint8_t h
Definition bl0906.h:2
uint8_t status
Definition bl0942.h:8
void feed_wdt()
Feed the task watchdog.
Fixed-capacity vector - sized once through init() or try_init(); push_back never reallocates This avo...
Definition helpers.h:621
Helper class to easily give an object a parent of type T.
Definition helpers.h:2004
Function-pointer-only templatable storage (4 bytes on 32-bit).
Definition automation.h:20
Primary TemplatableValue: stores either a constant value or a function pointer.
Definition automation.h:159
std::string get_response_header(const std::string &header_name)
Get the value of a response header by 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_
int status_code
-1 indicates no response received yet
bool is_chunked_
True if response uses chunked transfer encoding.
ESPDEPRECATED("Pass request_headers as std::vector<Header> instead of std::list. Removed in 2027.1.0.", "2026.7.0") std
std::shared_ptr< HttpContainer > post(const std::string &url, const std::string &body)
void set_useragent(const char *useragent)
std::shared_ptr< HttpContainer > get(const char *url)
std::shared_ptr< HttpContainer > start(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)
virtual 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)=0
std::shared_ptr< HttpContainer > post(const char *url, const std::string &body, const std::vector< Header > &request_headers, const std::vector< std::string > &lower_case_collect_headers)
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)
void set_follow_redirects(bool follow_redirects)
std::shared_ptr< HttpContainer > post(const char *url, const std::string &body, const std::vector< Header > &request_headers)
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_watchdog_timeout(uint32_t watchdog_timeout)
std::shared_ptr< HttpContainer > get(const char *url, const std::vector< Header > &request_headers)
std::shared_ptr< HttpContainer > post(const char *url, const std::string &body)
std::shared_ptr< HttpContainer > start(const char *url, const char *method, const std::string &body, const std::vector< Header > &request_headers)
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)
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)
std::shared_ptr< HttpContainer > get(const std::string &url, const std::vector< Header > &request_headers)
std::shared_ptr< HttpContainer > get(const char *url, const std::vector< Header > &request_headers, const std::vector< std::string > &lower_case_collect_headers)
void process(const std::shared_ptr< HttpContainer > &container, std::string &response_body)
void set_max_response_buffer_size(size_t max_response_buffer_size)
TEMPLATABLE_VALUE(std::string, url) TEMPLATABLE_VALUE(const char *
Trigger< std::shared_ptr< HttpContainer >, std::string &, Ts... > success_trigger_with_response_
method capture_response void init_request_headers(size_t count)
FixedVector< std::pair< const char *, TemplatableFn< 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 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 encode_json_(Ts... x, JsonObject root)
void add_request_header(const char *key, TemplatableFn< const char *, Ts... > value)
Trigger< std::shared_ptr< HttpContainer >, Ts... > * get_success_trigger()
HttpRequestSendAction(HttpRequestComponent *parent)
FixedVector< std::pair< const char *, TemplatableValue< std::string, Ts... > > > json_
void yield(void)
unsigned count
Definition gif.cpp:51
uint32_t buffer
Definition gif.cpp:50
mopeka_std_values val[3]
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)
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.
@ OK
Read completed successfully.
SerializationBuffer build_json(const json_build_t &f)
Build a JSON string with the provided json build function.
Definition json_util.cpp:19
constexpr float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Definition component.h:55
std::string str_lower_case(const std::string &str)
Convert the string to lower case.
if(written< 0)
Definition helpers.h:1195
void HOT delay(uint32_t ms)
Definition hal.cpp:85
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
Application App
Global storage of Application pointer - only one Application can exist.
STL namespace.
for(uint32_t *p=scan_start;p< stack_top &&p< scan_start+64 &&bt_count< MAX_BACKTRACE;p++)
static void uint32_t
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.
uint16_t x
Definition tt21100.cpp:5