[http_request] Fix usage of http response body (#17713)

Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
Grégoire Seux
2026-07-21 08:18:16 +12:00
committed by Jesse Hills
co-authored by J. Nick Koston
parent 5a86e26f68
commit 7738464f0b
2 changed files with 20 additions and 2 deletions
@@ -488,10 +488,10 @@ template<typename... Ts> class HttpRequestSendAction final : public Action<Ts...
body = this->body_.value(x...); body = this->body_.value(x...);
} }
if (!this->json_.empty()) { if (!this->json_.empty()) {
body = json::build_json([this, x...](JsonObject root) { this->encode_json_(x..., root); }); body = json::build_json([this, x...](JsonObject root) mutable { this->encode_json_(x..., root); });
} }
if (this->json_func_ != nullptr) { if (this->json_func_ != nullptr) {
body = json::build_json([this, x...](JsonObject root) { this->json_func_(x..., root); }); body = json::build_json([this, x...](JsonObject root) mutable { this->json_func_(x..., root); });
} }
std::vector<Header> request_headers; std::vector<Header> request_headers;
request_headers.reserve(this->request_headers_.size()); request_headers.reserve(this->request_headers_.size());
@@ -59,6 +59,24 @@ esphome:
id: test_regression_light id: test_regression_light
brightness: 100% brightness: 100%
effect: "None" effect: "None"
- http_request.get:
url: https://esphome.io
capture_response: true
on_response:
then:
# Regression test: http_request.post with json: (dict variant) inside
# on_response of a capture_response: true request puts std::string&
# (body) into the nested action's Ts..., which exposes a
# const-correctness bug in HttpRequestSendAction::play() where
# encode_json_ receives const copies of non-const reference args.
- http_request.post:
url: https://esphome.io
json:
status: "ok"
# Same with json: lambda variant, exercises json_func_ path
- http_request.post:
url: https://esphome.io
json: !lambda "root[\"status\"] = \"ok\";"
http_request: http_request:
useragent: esphome/tagreader useragent: esphome/tagreader