Skip to content

Next JS Api Routes - POST - Invalid Body response with request body payload #715

Open
@matthewgisonno

Description

@matthewgisonno

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.15.0

Plugin version

9.2.0

Node.js version

18.14.2

Operating system

macOS

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

13.2.1

Description

I'm trying to create a NextJS API Route for a POST method and send a payload. I can get regular page routes to work fine, even an API route with a GET method, works fine. I can also get a regular POST API Route to work, without a payload being sent

I created a API route located here: /src/pages/api/user.js ... This is based on the example located here: https://github.com/fastify/fastify-nextjs/blob/master/pages/api/user.js

I then updated my start file with the following:

  app.register(require("@fastify/nextjs")).after(() => {
    app.next("/hello");
    app.next("/api/user", { method: "POST" });
  });

When I open Post Man and try to send a POST request with the payload:

{
    "hello":"World"
}

the response I get is a 400 with "Invalid body" message.

I put together a SandBox located here:
https://codesandbox.io/s/lucid-rumple-ceetg2?file=/src/start.js:128-261

Steps to Reproduce

  1. Create API route located here: /src/pages/api/user.js
  2. Update start.js for API route
  3. Restart server
  4. Make a POST request to your API route with a JSON body payload: http://localhost/api/user
  5. See 400 Invalid Body response

Expected Behavior

Expected behavior is a NextJS API route should handle a POST just like it does in NextJS. Am I doing something wrong?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions