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 bool status = container->client_.begin(*stream_ptr, url.c_str());
75#elif defined(USE_RP2040)
77 container->client_.setInsecure();
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_);
95 container->client_.setUserAgent(this->
useragent_);
97 for (
const auto &header : request_headers) {
98 container->client_.addHeader(header.name.c_str(), header.value.c_str(),
false,
true);
102 const char *header_keys[lower_case_collect_headers.size()];
104 for (
auto const &header_name : lower_case_collect_headers) {
105 header_keys[index++] = header_name.c_str();
107 container->client_.collectHeaders(header_keys, index);
110 container->status_code = container->client_.sendRequest(method.c_str(), body.c_str());
112 if (container->status_code < 0) {
113#if defined(USE_ESP8266) && defined(USE_HTTP_REQUEST_ESP8266_HTTPS)
115 WiFiClientSecure *secure_client =
static_cast<WiFiClientSecure *
>(stream_ptr.get());
116 int last_error = secure_client->getLastSSLError();
118 if (last_error != 0) {
119 const LogString *error_msg;
120 switch (last_error) {
121 case ESP8266_SSL_ERR_OOM:
122 error_msg = LOG_STR(
"Unable to allocate buffer memory");
124 case BR_ERR_TOO_LARGE:
125 error_msg = LOG_STR(
"Incoming TLS record does not fit in receive buffer (BR_ERR_TOO_LARGE)");
128 error_msg = LOG_STR(
"Unknown SSL error");
131 ESP_LOGW(TAG,
"SSL failure: %s (Code: %d)", LOG_STR_ARG(error_msg), last_error);
132 if (last_error == ESP8266_SSL_ERR_OOM) {
133 ESP_LOGW(TAG,
"Configured TLS buffer sizes: %u/%u bytes, check max free heap block using the debug component",
137 ESP_LOGW(TAG,
"Connection failure with no error code");
142 ESP_LOGW(TAG,
"HTTP Request failed; URL: %s; Error: %s", url.c_str(),
143 HTTPClient::errorToString(container->status_code).c_str());
150 ESP_LOGE(TAG,
"HTTP Request failed; URL: %s; Code: %d", url.c_str(), container->status_code);
155 container->response_headers_.clear();
156 auto header_count = container->client_.headers();
157 for (
int i = 0; i < header_count; i++) {
158 const std::string header_name =
str_lower_case(container->client_.headerName(i).c_str());
160 std::string header_value = container->client_.header(i).c_str();
161 ESP_LOGD(TAG,
"Received response header, name: %s, value: %s", header_name.c_str(), header_value.c_str());
162 container->response_headers_.push_back({header_name, header_value});
172 int content_length = container->client_.getSize();
173 ESP_LOGD(TAG,
"Content-Length: %d", content_length);
174 container->content_length = (size_t) content_length;
176 container->set_chunked(content_length == -1);
207 WiFiClient *stream_ptr = this->
client_.getStreamPtr();
208 if (stream_ptr ==
nullptr) {
209 ESP_LOGE(TAG,
"Stream pointer vanished!");
210 return HTTP_ERROR_CONNECTION_CLOSED;
231 if (!stream_ptr->connected()) {
232 return HTTP_ERROR_CONNECTION_CLOSED;
238 int available_data = stream_ptr->available();
240 int bufsize = std::min({max_len, remaining, (size_t) available_data});
247 if (!stream_ptr->connected()) {
248 return HTTP_ERROR_CONNECTION_CLOSED;
254 int read_len = stream_ptr->readBytes(buf, bufsize);