ESPHome 2026.1.0-dev
Loading...
Searching...
No Matches
http_request.cpp
Go to the documentation of this file.
1#include "http_request.h"
2
3#include "esphome/core/log.h"
4
5#include <cinttypes>
6
8
9static const char *const TAG = "http_request";
10
12 ESP_LOGCONFIG(TAG,
13 "HTTP Request:\n"
14 " Timeout: %ums\n"
15 " User-Agent: %s\n"
16 " Follow redirects: %s\n"
17 " Redirect limit: %d",
18 this->timeout_, this->useragent_, YESNO(this->follow_redirects_), this->redirect_limit_);
19 if (this->watchdog_timeout_ > 0) {
20 ESP_LOGCONFIG(TAG, " Watchdog Timeout: %" PRIu32 "ms", this->watchdog_timeout_);
21 }
22}
23
24std::string HttpContainer::get_response_header(const std::string &header_name) {
25 auto response_headers = this->get_response_headers();
26 auto header_name_lower_case = str_lower_case(header_name);
27 if (response_headers.count(header_name_lower_case) == 0) {
28 ESP_LOGW(TAG, "No header with name %s found", header_name_lower_case.c_str());
29 return "";
30 } else {
31 auto values = response_headers[header_name_lower_case];
32 if (values.empty()) {
33 ESP_LOGE(TAG, "header with name %s returned an empty list, this shouldn't happen",
34 header_name_lower_case.c_str());
35 return "";
36 } else {
37 auto header_value = values.front();
38 ESP_LOGD(TAG, "Header with name %s found with value %s", header_name_lower_case.c_str(), header_value.c_str());
39 return header_value;
40 }
41 }
42}
43
44} // namespace esphome::http_request
std::string get_response_header(const std::string &header_name)
std::map< std::string, std::list< std::string > > get_response_headers()
Get response headers.
std::string str_lower_case(const std::string &str)
Convert the string to lower case.
Definition helpers.cpp:190