Repository navigation
Description
Is there an existing issue for this?
- I have searched the existing issues
Current behavior
Hi! π On the Fastify adapter, a handler that returns an object while the response has a Content-Type that is not JSON responds with HTTP 500:
@Get()
@Header('Content-Type', 'text/plain')
find() {
return { hello: 'world' };
}The error code is FST_ERR_REP_INVALID_PAYLOAD_TYPE, and the body is Attempted to send payload of invalid type 'object'. Expected a string or Buffer. The same happens with text/html, and when the handler sets the header with @Res({ passthrough: true }) and res.header().
On Express, the same code responds with HTTP 200. Express sends the object as JSON text and keeps the Content-Type of the handler.
What I found in the code:
FastifyAdapter.reply()gives the object toreply.send()without changing it (fastify-adapter.ts:767-779). The check in those lines changes theContent-Typeto JSON only for a body withstatusCode >= 400. It came from Fix Content-Type for unexpected errors in FastifyΒ #10474, which fixed Exception handler with Fastify leaks internals if content type is setΒ #8469, so a successful response does not get to it.- By default, Fastify serializes an object only when the reply has no
Content-Type, or when theContent-Typeis JSON. A custom reply serializer or anonSendhook can change that. Plain Fastify without Nest returns the same 500. - The Express adapter calls
response.json(body)for an object (express-adapter.ts:230), andresponse.json()keeps aContent-Typethat is already set.
I am not sure what the best fix is, so I would like to ask first π I tried a simple one locally: reply() calls JSON.stringify() on a plain object or an array when the Content-Type is not JSON. All the cases of the repro then pass. But it changes two setups that work today on Fastify:
| Setup | Today | With JSON.stringify() in reply() |
|---|---|---|
res.serializer(fn) with text/yaml |
200, fn gets the object |
200, fn gets a JSON string |
An onSend hook that turns the object into text, with text/csv |
200, the hook gets the object | 200, the hook gets a JSON string |
So the fix would need a way to keep those two cases. Another option is to keep the 500 and document that a non-JSON Content-Type needs a string or a Buffer on Fastify. I am happy to open a PR for the option that you prefer.
Minimum reproduction code
https://github.com/johnnyhuirilef/nestjs-repros/tree/main/fastify-object-non-json-content-type
Steps to reproduce
git clone https://github.com/johnnyhuirilef/nestjs-repros
cd nestjs-repros/fastify-object-non-json-content-type
npm install
npm run reproThe script runs four routes on Express and on Fastify. Output on Fastify:
=== fastify: text-plain ===
status: HTTP 500
content-type: "application/json; charset=utf-8"
expected: HTTP 200, "text/plain"
body: {"statusCode":500,"message":"Attempted to send payload of invalid type 'object'. Expected a string or Buffer."}
=== fastify: text-html ===
status: HTTP 500
content-type: "application/json; charset=utf-8"
expected: HTTP 200, "text/html"
body: {"statusCode":500,"message":"Attempted to send payload of invalid type 'object'. Expected a string or Buffer."}
=== fastify: passthrough-header ===
status: HTTP 500
content-type: "application/json; charset=utf-8"
expected: HTTP 200, "text/plain"
body: {"statusCode":500,"message":"Attempted to send payload of invalid type 'object'. Expected a string or Buffer."}
=== fastify: no-header ===
status: HTTP 200
content-type: "application/json; charset=utf-8"
expected: HTTP 200, "application/json"
body: {"hello":"world"}
RESULT: BUG: 3 of 8 cases did not send the object with the Content-Type of the handler.
All four Express cases return HTTP 200 with the Content-Type of the handler and the body {"hello":"world"}. The no-header case is a control. The README of the repro has the full output.
Expected behavior
The Fastify adapter answers like the Express adapter: HTTP 200, the object as JSON text, and the Content-Type of the handler. Or, if the 500 is intended, the docs say that a non-JSON Content-Type needs a string or a Buffer on Fastify.
NestJS version
12.2.0
Packages versions
@nestjs/common: 12.2.0
@nestjs/core: 12.2.0
@nestjs/platform-express: 12.2.0
@nestjs/platform-fastify: 12.2.0
fastify: 5.12.7
Node.js version
24.21.0
In which operating systems have you tested?
- macOS
- Windows
- Linux
Other
packages/platform-fastify on master (3a44abf) has no changes since v12.2.0.
Activity
docs: link the fastify-object-non-json-content-type repro to nestjs/nβ¦
kamilmysliwiec commentedon Oct 21, 2026
Thanks for the detailed write-up and for checking the options first! We're going to keep the current behaviour. Plain Fastify answers the same way, because it only serializes a payload when the
Content-Typeis JSON or not set. The Fastify adapter keeps that rule rather than changing what a reply serializer or anonSendhook receives.With a non-JSON
Content-Type, return a string or aBuffer(e.g.JSON.stringify(body)), or keep a JSON content type for object responses. Closing this one.