ESPHome 2025.6.2
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
7namespace esphome {
8namespace http_request {
9
10static const char *const TAG = "http_request";
11
13 ESP_LOGCONFIG(TAG,
14 "HTTP Request:\n"
15 " Timeout: %ums\n"
16 " User-Agent: %s\n"
17 " Follow redirects: %s\n"
18 " Redirect limit: %d",
19 this->timeout_, this->useragent_, YESNO(this->follow_redirects_), this->redirect_limit_);
20 if (this->watchdog_timeout_ > 0) {
21 ESP_LOGCONFIG(TAG, " Watchdog Timeout: %" PRIu32 "ms", this->watchdog_timeout_);
22 }
23}
24
25std::string HttpContainer::get_response_header(const std::string &header_name) {
26 auto response_headers = this->get_response_headers();
27 auto header_name_lower_case = str_lower_case(header_name);
28 if (response_headers.count(header_name_lower_case) == 0) {
29 ESP_LOGW(TAG, "No header with name %s found", header_name_lower_case.c_str());
30 return "";
31 } else {
32 auto values = response_headers[header_name_lower_case];
33 if (values.empty()) {
34 ESP_LOGE(TAG, "header with name %s returned an empty list, this shouldn't happen",
35 header_name_lower_case.c_str());
36 return "";
37 } else {
38 auto header_value = values.front();
39 ESP_LOGD(TAG, "Header with name %s found with value %s", header_name_lower_case.c_str(), header_value.c_str());
40 return header_value;
41 }
42 }
43}
44
45} // namespace http_request
46} // namespace esphome
std::string get_response_header(const std::string &header_name)
std::map< std::string, std::list< std::string > > get_response_headers()
Get response headers.
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string str_lower_case(const std::string &str)
Convert the string to lower case.
Definition helpers.cpp:290