18 const std::string &body,
19 const std::list<Header> &request_headers,
20 const std::set<std::string> &collect_headers) {
23 ESP_LOGW(TAG,
"HTTP Request failed; Not connected to network");
27 std::shared_ptr<HttpContainerArduino> container = std::make_shared<HttpContainerArduino>();
28 container->set_parent(
this);
32 bool secure = url.find(
"https:") != std::string::npos;
33 container->set_secure(secure);
38 container->client_.setFollowRedirects(HTTPC_FORCE_FOLLOW_REDIRECTS);
41 container->client_.setFollowRedirects(HTTPC_DISABLE_FOLLOW_REDIRECTS);
44#if defined(USE_ESP8266)
45 std::unique_ptr<WiFiClient> stream_ptr;
46#ifdef USE_HTTP_REQUEST_ESP8266_HTTPS
48 ESP_LOGV(TAG,
"ESP8266 HTTPS connection with WiFiClientSecure");
49 stream_ptr = std::make_unique<WiFiClientSecure>();
50 WiFiClientSecure *secure_client =
static_cast<WiFiClientSecure *
>(stream_ptr.get());
51 secure_client->setBufferSizes(512, 512);
52 secure_client->setInsecure();
54 stream_ptr = std::make_unique<WiFiClient>();
57 ESP_LOGV(TAG,
"ESP8266 HTTP connection with WiFiClient");
59 ESP_LOGE(TAG,
"Can't use HTTPS connection with esp8266_disable_ssl_support");
62 stream_ptr = std::make_unique<WiFiClient>();
65#if USE_ARDUINO_VERSION_CODE >= VERSION_CODE(3, 1, 0)
67 ESP_LOGW(TAG,
"Using HTTP on Arduino version >= 3.1 is **very** slow. Consider setting framework version to 3.0.2 "
68 "in your YAML, or use HTTPS");
71 bool status = container->client_.begin(*stream_ptr, url.c_str());
73#elif defined(USE_RP2040)
75 container->client_.setInsecure();
77 bool status = container->client_.begin(url.c_str());
78#elif defined(USE_ESP32)
79 bool status = container->client_.begin(url.c_str());
85 ESP_LOGW(TAG,
"HTTP Request failed; URL: %s", url.c_str());
91 container->client_.setReuse(
true);
92 container->client_.setTimeout(this->
timeout_);
94 container->client_.setConnectTimeout(this->
timeout_);
98 container->client_.setUserAgent(this->
useragent_);
100 for (
const auto &header : request_headers) {
101 container->client_.addHeader(header.name.c_str(), header.value.c_str(),
false,
true);
105 const char *header_keys[collect_headers.size()];
107 for (
auto const &header_name : collect_headers) {
108 header_keys[index++] = header_name.c_str();
110 container->client_.collectHeaders(header_keys, index);
113 container->status_code = container->client_.sendRequest(method.c_str(), body.c_str());
115 if (container->status_code < 0) {
116 ESP_LOGW(TAG,
"HTTP Request failed; URL: %s; Error: %s", url.c_str(),
117 HTTPClient::errorToString(container->status_code).c_str());
124 ESP_LOGE(TAG,
"HTTP Request failed; URL: %s; Code: %d", url.c_str(), container->status_code);
129 container->response_headers_ = {};
130 auto header_count = container->client_.headers();
131 for (
int i = 0; i < header_count; i++) {
132 const std::string header_name =
str_lower_case(container->client_.headerName(i).c_str());
133 if (collect_headers.count(header_name) > 0) {
134 std::string header_value = container->client_.header(i).c_str();
135 ESP_LOGD(TAG,
"Received response header, name: %s, value: %s", header_name.c_str(), header_value.c_str());
136 container->response_headers_[header_name].push_back(header_value);
140 int content_length = container->client_.getSize();
141 ESP_LOGD(TAG,
"Content-Length: %d", content_length);
142 container->content_length = (size_t) content_length;