diff --git a/esphome/components/http_request/http_request.cpp b/esphome/components/http_request/http_request.cpp index 64f3f97de9..4e1cfe94b3 100644 --- a/esphome/components/http_request/http_request.cpp +++ b/esphome/components/http_request/http_request.cpp @@ -120,10 +120,16 @@ void HttpRequestComponent::close() { } const char *HttpRequestComponent::get_string() { - // The static variable is here because HTTPClient::getString() returns a String on ESP32, and we need something to - // to keep a buffer alive. - static std::string str; - str = this->client_.getString().c_str(); +#if defined(ESP32) + // The static variable is here because HTTPClient::getString() returns a String on ESP32, + // and we need something to keep a buffer alive. + static String str; +#else + // However on ESP8266, HTTPClient::getString() returns a String& to a member variable. + // Leaving this the default so that any new platform either doesn't copy, or encounters a compilation error. + auto & +#endif + str = this->client_.getString(); return str.c_str(); }