question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

fastify example doesn't allow POST to pages/api handler

See original GitHub issue

Bug report

Describe the bug

Performing a POST to an API route when using a fastify server wrapper results in 400 Invalid body response.

To Reproduce

  • Use a fastify example found in examples/custom-server-fastify
  • Add the following in /pages/api/test.js:
module.exports = (req,res) => {
  const { body } = req;
  res.json({ping: body})
}

Make a client request:

curl -v -POST -H"Content-Type: application/json" -d'{"ping":true}' http://localhost:3000/api/test
*   Trying ::1...
* TCP_NODELAY set
* Connection failed
* connect to ::1 port 3000 failed: Connection refused
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 3000 (#0)
> POST /api/test HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.54.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 13
> 
* upload completely sent off: 13 out of 13 bytes
< HTTP/1.1 400 Invalid body
< Date: Tue, 07 Jan 2020 14:24:27 GMT
< Connection: keep-alive
< Transfer-Encoding: chunked
< 
* Connection #0 to host localhost left intact
Invalid body%

Expected behavior

My API route handles the request.

System information

  • Version of Next.js: 9.1.6
  • version of fastify: 2.11.0

Additional context

It’s clear the error is coming from here: https://github.com/zeit/next.js/blob/1fd57d22d5f435e63caf7d0fbe1bcd99be49ea52/packages/next/next-server/server/api-utils.ts#L81-L86

I suspect there’s a double-body parse happening; e.g. fastify is already parsing the body then next is trying to do the same a second time. I tried to disable json parsing in fastify by doing:

fastify.addContentTypeParser('*', function (req, done) {
  done()
})

as described here however it did not seem to have any effect. Still trying to work out a solution. I like the automatic route generation for modules under /pages/api so I would like to keep using that if possible. Otherwise I may have to move the API as plain fastify routes.

At a minimum the example README might want to add a caveat that API routes with content-body parsing will not work due to this interaction.

If I find a suitable workaround I’ll post an update here.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
chrskrchrcommented, Mar 25, 2021

This was fixed some time recently (now uses all).

@Timer - can you elaborate on what you mean by the “now uses all” part of your comment?

We’re in the process of migrating our custom Next.js server from the vanilla Node.js server to Fastify and ran into this same issue. It appears that Fastify’s content parser has already consumed the request body by the time that Next.js tries to parse it in api-utils.js before passing the req to the API request handler.

We were able to work around the issue by adding a no-op content parser in Fastify that leaves the body intact for these requests, but I’m curious if there’s a better way.

0reactions
balazsorban44commented, Jan 28, 2022

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

fastify example doesn't allow POST to pages/api handler #9978
Bug report Describe the bug Performing a POST to an API route when using a fastify server wrapper results in 400 Invalid body...
Read more >
Routes - Fastify
method : currently it supports 'DELETE' , 'GET' , 'HEAD' , 'PATCH' , 'POST' , 'PUT' , 'OPTIONS' , 'SEARCH' , 'TRACE' ,...
Read more >
How to: Deploy a GraphQL Mesh Gateway – GraphQL Mesh
First, let's ensure that mesh build will be run during deployment. ... 'OPTIONS'], async handler(req, reply) { // Second parameter adds Fastify's `req`...
Read more >
How to Create Your First REST API with Fastify - SitePoint
An introduction to Fastify, showing how to set up a Fastify API, define API routes, ... Let's get started with this tutorial.
Read more >
Build a CRUD API with Fastify - DEV Community ‍ ‍
For example, let's make a schema for our get request above. ... In handlers/posts.js, create a function called getPostsHandler with req and ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found