From 45b8810ab8a888a1cf43aae308770d159a87e25f Mon Sep 17 00:00:00 2001 From: Craig Andrews Date: Sun, 9 Feb 2025 21:55:16 -0500 Subject: [PATCH] [online_image] Set Accept header (#8216) --- .../components/online_image/online_image.cpp | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/esphome/components/online_image/online_image.cpp b/esphome/components/online_image/online_image.cpp index b08c14b721..da0e88a904 100644 --- a/esphome/components/online_image/online_image.cpp +++ b/esphome/components/online_image/online_image.cpp @@ -100,7 +100,35 @@ void OnlineImage::update() { } ESP_LOGI(TAG, "Updating image %s", this->url_.c_str()); - this->downloader_ = this->parent_->get(this->url_); + std::list headers = {}; + + http_request::Header accept_header; + accept_header.name = "Accept"; + std::string accept_mime_type; + switch (this->format_) { +#ifdef USE_ONLINE_IMAGE_BMP_SUPPORT + case ImageFormat::BMP: + accept_mime_type = "image/bmp"; + break; +#endif // ONLINE_IMAGE_BMP_SUPPORT +#ifdef USE_ONLINE_IMAGE_JPEG_SUPPORT + case ImageFormat::JPEG: + accept_mime_type = "image/jpeg"; + break; +#endif // USE_ONLINE_IMAGE_JPEG_SUPPORT +#ifdef USE_ONLINE_IMAGE_PNG_SUPPORT + case ImageFormat::PNG: + accept_mime_type = "image/png"; + break; +#endif // ONLINE_IMAGE_PNG_SUPPORT + default: + accept_mime_type = "image/*"; + } + accept_header.value = (accept_mime_type + ",*/*;q=0.8").c_str(); + + headers.push_back(accept_header); + + this->downloader_ = this->parent_->get(this->url_, headers); if (this->downloader_ == nullptr) { ESP_LOGE(TAG, "Download failed.");