17 const std::string &body,
18 const std::list<Header> &request_headers,
19 const 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());
82 ESP_LOGW(TAG,
"HTTP Request failed; URL: %s", url.c_str());
88 container->client_.setReuse(
true);
89 container->client_.setTimeout(this->
timeout_);
92 container->client_.setUserAgent(this->
useragent_);
94 for (
const auto &header : request_headers) {
95 container->client_.addHeader(header.name.c_str(), header.value.c_str(),
false,
true);
99 const char *header_keys[collect_headers.size()];
101 for (
auto const &header_name : collect_headers) {
102 header_keys[index++] = header_name.c_str();
104 container->client_.collectHeaders(header_keys, index);
107 container->status_code = container->client_.sendRequest(method.c_str(), body.c_str());
109 if (container->status_code < 0) {
110 ESP_LOGW(TAG,
"HTTP Request failed; URL: %s; Error: %s", url.c_str(),
111 HTTPClient::errorToString(container->status_code).c_str());
118 ESP_LOGE(TAG,
"HTTP Request failed; URL: %s; Code: %d", url.c_str(), container->status_code);
123 container->response_headers_ = {};
124 auto header_count = container->client_.headers();
125 for (
int i = 0; i < header_count; i++) {
126 const std::string header_name =
str_lower_case(container->client_.headerName(i).c_str());
127 if (collect_headers.count(header_name) > 0) {
128 std::string header_value = container->client_.header(i).c_str();
129 ESP_LOGD(TAG,
"Received response header, name: %s, value: %s", header_name.c_str(), header_value.c_str());
130 container->response_headers_[header_name].push_back(header_value);
134 int content_length = container->client_.getSize();
135 ESP_LOGD(TAG,
"Content-Length: %d", content_length);
136 container->content_length = (size_t) content_length;