Skip to content

Positive content-length with return Promise.resolve(null) and 204 status code #5003

@greguz

Description

@greguz

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.22.0

Plugin version

No response

Node.js version

18.16.1

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

6.4.11-arch2-1 (64-bit)

Description

Hi everyone.
I'm having Postman hanging with some 204 responses.
I suspect the problem could be the positively-valued content-length response header. I've recreated the case without any trouble with a bare-bone Fastify instance (see Steps to Reproduce).

Having reply.code(204):

  • when I'm using an async (Promise) handler that resolves with null or undefined, the response header content-length: 4 will be present (hanging Postman and other HTTP clients).
  • when I call reply.send(), with or without promises, the content-length header is omitted from the response.

Steps to Reproduce

// Node.js v18.16.1
import Fastify from 'fastify' // v4.22.0

const app = Fastify({
  logger: true,
})

app.route({
  method: 'POST',
  url: '*',
  async handler(request, reply) {
    reply.code(204)

    // Replace with `reply.send()`, and everything works.
    return null
  },
})

await app.listen({
  port: 3000,
})

Run code above.
Run wget -q -S -O- --post-data='{}' --header='Content-Type:application/json' 'http://127.0.0.1:3000/json'

My output is:

  HTTP/1.1 204 No Content
  content-type: application/json; charset=utf-8
  content-length: 4
  Date: Tue, 29 Aug 2023 12:58:12 GMT
  Connection: keep-alive
  Keep-Alive: timeout=72

Expected Behavior

Response header content-length should be 0 or omitted when 204 status code is set.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions