28 const std::vector<Header> &request_headers,
29 const std::vector<std::string> &lower_case_collect_headers) {
32 ESP_LOGW(TAG,
"HTTP Request failed; Not connected to network");
36 std::shared_ptr<HttpContainerArduino> container = std::make_shared<HttpContainerArduino>();
37 container->set_parent(
this);
41 bool secure = strstr(url,
"https:") !=
nullptr;
42 container->set_secure(secure);
47 container->client_.setFollowRedirects(HTTPC_FORCE_FOLLOW_REDIRECTS);
50 container->client_.setFollowRedirects(HTTPC_DISABLE_FOLLOW_REDIRECTS);
53#if defined(USE_ESP8266)
54 std::unique_ptr<WiFiClient> stream_ptr;
55#ifdef USE_HTTP_REQUEST_ESP8266_HTTPS
57 ESP_LOGV(TAG,
"ESP8266 HTTPS connection with WiFiClientSecure");
58 stream_ptr = std::make_unique<WiFiClientSecure>();
59 WiFiClientSecure *secure_client =
static_cast<WiFiClientSecure *
>(stream_ptr.get());
61 secure_client->setInsecure();
63 stream_ptr = std::make_unique<WiFiClient>();
66 ESP_LOGV(TAG,
"ESP8266 HTTP connection with WiFiClient");
68 ESP_LOGE(TAG,
"Can't use HTTPS connection with esp8266_disable_ssl_support");
71 stream_ptr = std::make_unique<WiFiClient>();
74 bool status = container->client_.begin(*stream_ptr, url);
78 container->client_.setInsecure();
80 bool status = container->client_.begin(url);
86 ESP_LOGW(TAG,
"HTTP Request failed; URL: %s", url);
92 container->client_.setReuse(
true);
93 container->client_.setTimeout(this->
timeout_);
96 container->client_.setUserAgent(this->
useragent_);
98 for (
const auto &header : request_headers) {
99 container->client_.addHeader(header.name.c_str(), header.value.c_str(),
false,
true);
103 const char *header_keys[lower_case_collect_headers.size()];
105 for (
auto const &header_name : lower_case_collect_headers) {
106 header_keys[index++] = header_name.c_str();
108 container->client_.collectHeaders(header_keys, index);
111 container->status_code = container->client_.sendRequest(method, body.c_str());
113 if (container->status_code < 0) {
114#if defined(USE_ESP8266) && defined(USE_HTTP_REQUEST_ESP8266_HTTPS)
116 WiFiClientSecure *secure_client =
static_cast<WiFiClientSecure *
>(stream_ptr.get());
117 int last_error = secure_client->getLastSSLError();
119 if (last_error != 0) {
120 const LogString *error_msg;
121 switch (last_error) {
122 case ESP8266_SSL_ERR_OOM:
123 error_msg = LOG_STR(
"Unable to allocate buffer memory");
125 case BR_ERR_TOO_LARGE:
126 error_msg = LOG_STR(
"Incoming TLS record does not fit in receive buffer (BR_ERR_TOO_LARGE)");
129 error_msg = LOG_STR(
"Unknown SSL error");
132 ESP_LOGW(TAG,
"SSL failure: %s (Code: %d)", LOG_STR_ARG(error_msg), last_error);
133 if (last_error == ESP8266_SSL_ERR_OOM) {
134 ESP_LOGW(TAG,
"Configured TLS buffer sizes: %u/%u bytes, check max free heap block using the debug component",
138 ESP_LOGW(TAG,
"Connection failure with no error code");
143 ESP_LOGW(TAG,
"HTTP Request failed; URL: %s; Error: %s", url,
144 HTTPClient::errorToString(container->status_code).c_str());
151 ESP_LOGE(TAG,
"HTTP Request failed; URL: %s; Code: %d", url, container->status_code);
156 container->response_headers_.clear();
157 auto header_count = container->client_.headers();
158 for (
int i = 0; i < header_count; i++) {
159 const std::string header_name =
str_lower_case(container->client_.headerName(i).c_str());
161 std::string header_value = container->client_.header(i).c_str();
162 ESP_LOGD(TAG,
"Received response header, name: %s, value: %s", header_name.c_str(), header_value.c_str());
163 container->response_headers_.push_back({header_name, header_value});
173 int content_length = container->client_.getSize();
174 ESP_LOGD(TAG,
"Content-Length: %d", content_length);
175 container->content_length = (size_t) content_length;
177 container->set_chunked(content_length == -1);
208 WiFiClient *stream_ptr = this->
client_.getStreamPtr();
209 if (stream_ptr ==
nullptr) {
210 ESP_LOGE(TAG,
"Stream pointer vanished!");
211 return HTTP_ERROR_CONNECTION_CLOSED;
232 if (!stream_ptr->connected()) {
233 return HTTP_ERROR_CONNECTION_CLOSED;
239 int available_data = stream_ptr->available();
241 int bufsize = std::min({max_len, remaining, (size_t) available_data});
248 if (!stream_ptr->connected()) {
249 return HTTP_ERROR_CONNECTION_CLOSED;
255 int read_len = stream_ptr->readBytes(buf, bufsize);