Skip to content

Returning an object with a non-JSON Content-Type responds 500 on the Fastify adapterΒ #18208

Closed as not planned

Description

@johnnyhuirilef

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:

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 repro

The 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

  1. kamilmysliwiec commented on Oct 21, 2026

    @kamilmysliwiec
    Member

    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-Type is JSON or not set. The Fastify adapter keeps that rule rather than changing what a reply serializer or an onSend hook receives.

    With a non-JSON Content-Type, return a string or a Buffer (e.g. JSON.stringify(body)), or keep a JSON content type for object responses. Closing this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs triageThis issue has not been looked into

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions