13 namespace http_request {
15 static const char *
const TAG =
"http_request.arduino";
18 std::list<Header> headers) {
21 ESP_LOGW(TAG,
"HTTP Request failed; Not connected to network");
25 std::shared_ptr<HttpContainerArduino> container = std::make_shared<HttpContainerArduino>();
26 container->set_parent(
this);
30 bool secure = url.find(
"https:") != std::string::npos;
31 container->set_secure(secure);
36 container->client_.setFollowRedirects(HTTPC_FORCE_FOLLOW_REDIRECTS);
39 container->client_.setFollowRedirects(HTTPC_DISABLE_FOLLOW_REDIRECTS);
42 #if defined(USE_ESP8266) 43 std::unique_ptr<WiFiClient> stream_ptr;
44 #ifdef USE_HTTP_REQUEST_ESP8266_HTTPS 46 ESP_LOGV(TAG,
"ESP8266 HTTPS connection with WiFiClientSecure");
47 stream_ptr = std::make_unique<WiFiClientSecure>();
48 WiFiClientSecure *secure_client =
static_cast<WiFiClientSecure *
>(stream_ptr.get());
49 secure_client->setBufferSizes(512, 512);
50 secure_client->setInsecure();
52 stream_ptr = std::make_unique<WiFiClient>();
55 ESP_LOGV(TAG,
"ESP8266 HTTP connection with WiFiClient");
57 ESP_LOGE(TAG,
"Can't use HTTPS connection with esp8266_disable_ssl_support");
60 stream_ptr = std::make_unique<WiFiClient>();
61 #endif // USE_HTTP_REQUEST_ESP8266_HTTPS 63 #if USE_ARDUINO_VERSION_CODE >= VERSION_CODE(3, 1, 0) // && USE_ARDUINO_VERSION_CODE < VERSION_CODE(?, ?, ?) 65 ESP_LOGW(TAG,
"Using HTTP on Arduino version >= 3.1 is **very** slow. Consider setting framework version to 3.0.2 " 66 "in your YAML, or use HTTPS");
68 #endif // USE_ARDUINO_VERSION_CODE 69 bool status = container->client_.begin(*stream_ptr, url.c_str());
71 #elif defined(USE_RP2040) 73 container->client_.setInsecure();
75 bool status = container->client_.begin(url.c_str());
76 #elif defined(USE_ESP32) 77 bool status = container->client_.begin(url.c_str());
83 ESP_LOGW(TAG,
"HTTP Request failed; URL: %s", url.c_str());
89 container->client_.setReuse(
true);
90 container->client_.setTimeout(this->
timeout_);
91 #if defined(USE_ESP32) 92 container->client_.setConnectTimeout(this->
timeout_);
96 container->client_.setUserAgent(this->
useragent_);
98 for (
const auto &header : headers) {
99 container->client_.addHeader(header.name, header.value,
false,
true);
103 static const char *header_keys[] = {
"Content-Length",
"Content-Type"};
104 static const size_t HEADER_COUNT =
sizeof(header_keys) /
sizeof(header_keys[0]);
105 container->client_.collectHeaders(header_keys, HEADER_COUNT);
107 container->status_code = container->client_.sendRequest(method.c_str(), body.c_str());
108 if (container->status_code < 0) {
109 ESP_LOGW(TAG,
"HTTP Request failed; URL: %s; Error: %s", url.c_str(),
110 HTTPClient::errorToString(container->status_code).c_str());
116 if (container->status_code < 200 || container->status_code >= 300) {
117 ESP_LOGE(TAG,
"HTTP Request failed; URL: %s; Code: %d", url.c_str(), container->status_code);
123 int content_length = container->client_.getSize();
124 ESP_LOGD(TAG,
"Content-Length: %d", content_length);
125 container->content_length = (size_t) content_length;
135 WiFiClient *stream_ptr = this->client_.getStreamPtr();
136 if (stream_ptr ==
nullptr) {
137 ESP_LOGE(TAG,
"Stream pointer vanished!");
141 int available_data = stream_ptr->available();
142 int bufsize = std::min(max_len, std::min(this->content_length - this->bytes_read_, (
size_t) available_data));
150 int read_len = stream_ptr->readBytes(buf, bufsize);
151 this->bytes_read_ += read_len;
166 #endif // USE_ARDUINO
uint32_t get_watchdog_timeout() const
bool is_connected()
Return whether the node is connected to the network (through wifi, eth, ...)
uint32_t IRAM_ATTR HOT millis()
void status_momentary_error(const std::string &name, uint32_t length=5000)
int read(uint8_t *buf, size_t max_len) override
Application App
Global storage of Application pointer - only one Application can exist.
std::shared_ptr< HttpContainer > start(std::string url, std::string method, std::string body, std::list< Header > headers) override
Implementation of SPI Controller mode.