26 const std::string &body,
27 const std::vector<Header> &request_headers,
28 const std::vector<std::string> &lower_case_collect_headers) {
31 ESP_LOGW(TAG,
"HTTP Request failed; Not connected to network");
35 std::shared_ptr<HttpContainerArduino> container = std::make_shared<HttpContainerArduino>();
36 container->set_parent(
this);
40 bool secure = url.find(
"https:") != std::string::npos;
41 container->set_secure(secure);
46 container->client_.setFollowRedirects(HTTPC_FORCE_FOLLOW_REDIRECTS);
49 container->client_.setFollowRedirects(HTTPC_DISABLE_FOLLOW_REDIRECTS);
52#if defined(USE_ESP8266)
53 std::unique_ptr<WiFiClient> stream_ptr;
54#ifdef USE_HTTP_REQUEST_ESP8266_HTTPS
56 ESP_LOGV(TAG,
"ESP8266 HTTPS connection with WiFiClientSecure");
57 stream_ptr = std::make_unique<WiFiClientSecure>();
58 WiFiClientSecure *secure_client =
static_cast<WiFiClientSecure *
>(stream_ptr.get());
60 secure_client->setInsecure();
62 stream_ptr = std::make_unique<WiFiClient>();
65 ESP_LOGV(TAG,
"ESP8266 HTTP connection with WiFiClient");
67 ESP_LOGE(TAG,
"Can't use HTTPS connection with esp8266_disable_ssl_support");
70 stream_ptr = std::make_unique<WiFiClient>();
73#if USE_ARDUINO_VERSION_CODE >= VERSION_CODE(3, 1, 0)
75 ESP_LOGW(TAG,
"Using HTTP on Arduino version >= 3.1 is **very** slow. Consider setting framework version to 3.0.2 "
76 "in your YAML, or use HTTPS");
79 bool status = container->client_.begin(*stream_ptr, url.c_str());
81#elif defined(USE_RP2040)
83 container->client_.setInsecure();
85 bool status = container->client_.begin(url.c_str());
91 ESP_LOGW(TAG,
"HTTP Request failed; URL: %s", url.c_str());
97 container->client_.setReuse(
true);
98 container->client_.setTimeout(this->
timeout_);
101 container->client_.setUserAgent(this->
useragent_);
103 for (
const auto &header : request_headers) {
104 container->client_.addHeader(header.name.c_str(), header.value.c_str(),
false,
true);
108 const char *header_keys[lower_case_collect_headers.size()];
110 for (
auto const &header_name : lower_case_collect_headers) {
111 header_keys[index++] = header_name.c_str();
113 container->client_.collectHeaders(header_keys, index);
116 container->status_code = container->client_.sendRequest(method.c_str(), body.c_str());
118 if (container->status_code < 0) {
119#if defined(USE_ESP8266) && defined(USE_HTTP_REQUEST_ESP8266_HTTPS)
121 WiFiClientSecure *secure_client =
static_cast<WiFiClientSecure *
>(stream_ptr.get());
122 int last_error = secure_client->getLastSSLError();
124 if (last_error != 0) {
125 const LogString *error_msg;
126 switch (last_error) {
127 case ESP8266_SSL_ERR_OOM:
128 error_msg = LOG_STR(
"Unable to allocate buffer memory");
130 case BR_ERR_TOO_LARGE:
131 error_msg = LOG_STR(
"Incoming TLS record does not fit in receive buffer (BR_ERR_TOO_LARGE)");
134 error_msg = LOG_STR(
"Unknown SSL error");
137 ESP_LOGW(TAG,
"SSL failure: %s (Code: %d)", LOG_STR_ARG(error_msg), last_error);
138 if (last_error == ESP8266_SSL_ERR_OOM) {
139 ESP_LOGW(TAG,
"Configured TLS buffer sizes: %u/%u bytes, check max free heap block using the debug component",
143 ESP_LOGW(TAG,
"Connection failure with no error code");
148 ESP_LOGW(TAG,
"HTTP Request failed; URL: %s; Error: %s", url.c_str(),
149 HTTPClient::errorToString(container->status_code).c_str());
156 ESP_LOGE(TAG,
"HTTP Request failed; URL: %s; Code: %d", url.c_str(), container->status_code);
161 container->response_headers_.clear();
162 auto header_count = container->client_.headers();
163 for (
int i = 0; i < header_count; i++) {
164 const std::string header_name =
str_lower_case(container->client_.headerName(i).c_str());
166 std::string header_value = container->client_.header(i).c_str();
167 ESP_LOGD(TAG,
"Received response header, name: %s, value: %s", header_name.c_str(), header_value.c_str());
168 container->response_headers_.push_back({header_name, header_value});
178 int content_length = container->client_.getSize();
179 ESP_LOGD(TAG,
"Content-Length: %d", content_length);
180 container->content_length = (size_t) content_length;
182 container->set_chunked(content_length == -1);
213 WiFiClient *stream_ptr = this->
client_.getStreamPtr();
214 if (stream_ptr ==
nullptr) {
215 ESP_LOGE(TAG,
"Stream pointer vanished!");
216 return HTTP_ERROR_CONNECTION_CLOSED;
237 if (!stream_ptr->connected()) {
238 return HTTP_ERROR_CONNECTION_CLOSED;
244 int available_data = stream_ptr->available();
246 int bufsize = std::min(max_len, std::min(remaining, (
size_t) available_data));
253 if (!stream_ptr->connected()) {
254 return HTTP_ERROR_CONNECTION_CLOSED;
260 int read_len = stream_ptr->readBytes(buf, bufsize);