From 2d2eab9199be548ebe537f94c06ed1c5fd393b7f Mon Sep 17 00:00:00 2001 From: Mark Scherer Date: Mon, 22 Jun 2026 23:46:16 +0200 Subject: [PATCH 1/2] Document that withStringBody() requires returning the response or disabling auto-render --- docs/en/controllers/request-response.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/docs/en/controllers/request-response.md b/docs/en/controllers/request-response.md index 6ad39d1a92..61edd4f6ad 100644 --- a/docs/en/controllers/request-response.md +++ b/docs/en/controllers/request-response.md @@ -831,6 +831,30 @@ $response = $response->withType('application/json') ->withStringBody(json_encode(['Foo' => 'bar'])); ``` +::: warning +Setting a string body alone is not enough to send it. If your action neither +returns the response nor disables view rendering, the controller still calls +`render()` and overwrites the body you set. To make the string body take effect, +either return the response from the action: + +```php +public function export() +{ + return $this->response->withStringBody('My Body'); +} +``` + +or set it and disable auto-render: + +```php +public function export() +{ + $this->setResponse($this->response->withStringBody('My Body')); + $this->disableAutoRender(); +} +``` +::: + `method` Cake\\Http\\Response::**withBody**(StreamInterface $body): static To set the response body, use the `withBody()` method, which is provided by the From f44735dd8ccd4867803b9a937cf84fc554231dcc Mon Sep 17 00:00:00 2001 From: Mark Scherer Date: Mon, 22 Jun 2026 23:47:27 +0200 Subject: [PATCH 2/2] Fix MD031 blank line around fenced code block --- docs/en/controllers/request-response.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/en/controllers/request-response.md b/docs/en/controllers/request-response.md index 61edd4f6ad..3eba5b8027 100644 --- a/docs/en/controllers/request-response.md +++ b/docs/en/controllers/request-response.md @@ -853,6 +853,7 @@ public function export() $this->disableAutoRender(); } ``` + ::: `method` Cake\\Http\\Response::**withBody**(StreamInterface $body): static