18 std::list<Header> request_headers,
19 std::set<std::string> collect_headers) {
22 ESP_LOGW(TAG,
"HTTP Request failed; Not connected to network");
26 std::shared_ptr<HttpContainerArduino> container = std::make_shared<HttpContainerArduino>();
27 container->set_parent(
this);
31 bool secure = url.find(
"https:") != std::string::npos;
32 container->set_secure(secure);
37 container->client_.setFollowRedirects(HTTPC_FORCE_FOLLOW_REDIRECTS);
40 container->client_.setFollowRedirects(HTTPC_DISABLE_FOLLOW_REDIRECTS);
43#if defined(USE_ESP8266)
44 std::unique_ptr<WiFiClient> stream_ptr;
45#ifdef USE_HTTP_REQUEST_ESP8266_HTTPS
47 ESP_LOGV(TAG,
"ESP8266 HTTPS connection with WiFiClientSecure");
48 stream_ptr = std::make_unique<WiFiClientSecure>();
49 WiFiClientSecure *secure_client =
static_cast<WiFiClientSecure *
>(stream_ptr.get());
50 secure_client->setBufferSizes(512, 512);
51 secure_client->setInsecure();
53 stream_ptr = std::make_unique<WiFiClient>();
56 ESP_LOGV(TAG,
"ESP8266 HTTP connection with WiFiClient");
58 ESP_LOGE(TAG,
"Can't use HTTPS connection with esp8266_disable_ssl_support");
61 stream_ptr = std::make_unique<WiFiClient>();
64#if USE_ARDUINO_VERSION_CODE >= VERSION_CODE(3, 1, 0)
66 ESP_LOGW(TAG,
"Using HTTP on Arduino version >= 3.1 is **very** slow. Consider setting framework version to 3.0.2 "
67 "in your YAML, or use HTTPS");
70 bool status = container->client_.begin(*stream_ptr, url.c_str());
72#elif defined(USE_RP2040)
74 container->client_.setInsecure();
76 bool status = container->client_.begin(url.c_str());
77#elif defined(USE_ESP32)
78 bool status = container->client_.begin(url.c_str());
84 ESP_LOGW(TAG,
"HTTP Request failed; URL: %s", url.c_str());
90 container->client_.setReuse(
true);
91 container->client_.setTimeout(this->
timeout_);
93 container->client_.setConnectTimeout(this->
timeout_);
97 container->client_.setUserAgent(this->
useragent_);
99 for (
const auto &header : request_headers) {
100 container->client_.addHeader(header.name.c_str(), header.value.c_str(),
false,
true);
104 const char *header_keys[collect_headers.size()];
106 for (
auto const &header_name : collect_headers) {
107 header_keys[index++] = header_name.c_str();
109 container->client_.collectHeaders(header_keys, index);
112 container->status_code = container->client_.sendRequest(method.c_str(), body.c_str());
114 if (container->status_code < 0) {
115 ESP_LOGW(TAG,
"HTTP Request failed; URL: %s; Error: %s", url.c_str(),
116 HTTPClient::errorToString(container->status_code).c_str());
123 ESP_LOGE(TAG,
"HTTP Request failed; URL: %s; Code: %d", url.c_str(), container->status_code);
128 container->response_headers_ = {};
129 auto header_count = container->client_.headers();
130 for (
int i = 0; i < header_count; i++) {
131 const std::string header_name =
str_lower_case(container->client_.headerName(i).c_str());
132 if (collect_headers.count(header_name) > 0) {
133 std::string header_value = container->client_.header(i).c_str();
134 ESP_LOGD(TAG,
"Received response header, name: %s, value: %s", header_name.c_str(), header_value.c_str());
135 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;