29 const std::vector<Header> &request_headers,
30 const std::vector<std::string> &lower_case_collect_headers,
34 ESP_LOGW(TAG,
"HTTP Request failed; Not connected to network");
38 std::shared_ptr<HttpContainerArduino> container = std::make_shared<HttpContainerArduino>();
39 container->set_parent(
this);
44 bool secure = ESPHOME_strncasecmp_P(url, ESPHOME_PSTR(
"https:"), 6) == 0;
45 container->set_secure(secure);
51 container->client_.setFollowRedirects(HTTPC_FORCE_FOLLOW_REDIRECTS);
54 container->client_.setFollowRedirects(HTTPC_DISABLE_FOLLOW_REDIRECTS);
57#if defined(USE_ESP8266)
58 std::unique_ptr<WiFiClient> stream_ptr;
59#ifdef USE_HTTP_REQUEST_ESP8266_HTTPS
61 ESP_LOGV(TAG,
"ESP8266 HTTPS connection with WiFiClientSecure");
62 stream_ptr = std::make_unique<WiFiClientSecure>();
63 WiFiClientSecure *secure_client =
static_cast<WiFiClientSecure *
>(stream_ptr.get());
65 secure_client->setInsecure();
67 stream_ptr = std::make_unique<WiFiClient>();
70 ESP_LOGV(TAG,
"ESP8266 HTTP connection with WiFiClient");
72 ESP_LOGE(TAG,
"Can't use HTTPS connection with esp8266_disable_ssl_support");
75 stream_ptr = std::make_unique<WiFiClient>();
78 bool status = container->client_.begin(*stream_ptr, url);
82 container->client_.setInsecure();
84 bool status = container->client_.begin(url);
90 ESP_LOGW(TAG,
"HTTP Request failed; URL: %s", url);
96 container->client_.setReuse(
true);
97 container->client_.setTimeout(timeout_ms);
100 container->client_.setUserAgent(this->
useragent_);
102 for (
const auto &header : request_headers) {
103 container->client_.addHeader(header.name.c_str(), header.value.c_str(),
false,
true);
107 const char *header_keys[lower_case_collect_headers.size()];
109 for (
auto const &header_name : lower_case_collect_headers) {
110 header_keys[index++] = header_name.c_str();
112 container->client_.collectHeaders(header_keys, index);
115 container->status_code = container->client_.sendRequest(method, body.c_str());
117 if (container->status_code < 0) {
118#if defined(USE_ESP8266) && defined(USE_HTTP_REQUEST_ESP8266_HTTPS)
120 WiFiClientSecure *secure_client =
static_cast<WiFiClientSecure *
>(stream_ptr.get());
121 int last_error = secure_client->getLastSSLError();
123 if (last_error != 0) {
124 const LogString *error_msg;
125 switch (last_error) {
126 case ESP8266_SSL_ERR_OOM:
127 error_msg = LOG_STR(
"Unable to allocate buffer memory");
129 case BR_ERR_TOO_LARGE:
130 error_msg = LOG_STR(
"Incoming TLS record does not fit in receive buffer (BR_ERR_TOO_LARGE)");
133 error_msg = LOG_STR(
"Unknown SSL error");
136 ESP_LOGW(TAG,
"SSL failure: %s (Code: %d)", LOG_STR_ARG(error_msg), last_error);
137 if (last_error == ESP8266_SSL_ERR_OOM) {
138 ESP_LOGW(TAG,
"Configured TLS buffer sizes: %u/%u bytes, check max free heap block using the debug component",
142 ESP_LOGW(TAG,
"Connection failure with no error code");
147 ESP_LOGW(TAG,
"HTTP Request failed; URL: %s; Error: %s", url,
148 HTTPClient::errorToString(container->status_code).c_str());
155 ESP_LOGE(TAG,
"HTTP Request failed; URL: %s; Code: %d", url, container->status_code);
160 container->response_headers_.clear();
161 auto header_count = container->client_.headers();
162 for (
int i = 0; i < header_count; i++) {
163 const std::string header_name =
str_lower_case(container->client_.headerName(i).c_str());
165 std::string header_value = container->client_.header(i).c_str();
166 ESP_LOGD(TAG,
"Received response header, name: %s, value: %s", header_name.c_str(), header_value.c_str());
167 container->response_headers_.push_back({header_name, header_value});
177 int content_length = container->client_.getSize();
178 ESP_LOGD(TAG,
"Content-Length: %d", content_length);
179 container->content_length = (size_t) content_length;
181 container->set_chunked(content_length == -1);
212 WiFiClient *stream_ptr = this->
client_.getStreamPtr();
213 if (stream_ptr ==
nullptr) {
214 ESP_LOGE(TAG,
"Stream pointer vanished!");
215 return HTTP_ERROR_CONNECTION_CLOSED;
236 if (!stream_ptr->connected()) {
237 return HTTP_ERROR_CONNECTION_CLOSED;
243 int available_data = stream_ptr->available();
245 int bufsize = std::min({max_len, remaining, (size_t) available_data});
252 if (!stream_ptr->connected()) {
253 return HTTP_ERROR_CONNECTION_CLOSED;
259 int read_len = stream_ptr->readBytes(buf, bufsize);