Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

await req.json() in edge middleware returns undefined #32509

Closed
switz opened this issue Dec 14, 2021 · 2 comments
Closed

await req.json() in edge middleware returns undefined #32509

switz opened this issue Dec 14, 2021 · 2 comments
Labels
Middleware Related to Next.js Middleware.

Comments

@switz
Copy link

switz commented Dec 14, 2021

What version of Next.js are you using?

12.0.7

What version of Node.js are you using?

14 LTS

What browser are you using?

Firefox

What operating system are you using?

Mac 12.1

How are you deploying your application?

locally

Describe the Bug

I have a _middleware.ts file:

export async function middleware(req: NextRequest) {
  if (req.method !== 'POST') {
    return error('POST only...');
  }

  const body = await req.json().catch((err) => {
    console.log('token json', err);
  });
  console.log({ body });


  // etc...

}

This logs out:

token json TypeError: invalid json body reason: Unexpected end of JSON input
    at NextRequestHint.json (evalmachine.<anonymous>:172:19)
    at async Object.middleware [as handler] (webpack-internal:///./src/pages/api/token/_middleware.ts:50:16)
    at async adapter (webpack-internal:///../../node_modules/next/dist/server/web/adapter.js:30:22)
    at async DevServer.runMiddleware (/Users/me/app/node_modules/next/dist/server/next-server.js:448:26)
    at async DevServer.runMiddleware (/Users/me/app/node_modules/next/dist/server/dev/next-dev-server.js:400:28)
    at async Object.fn (/Users/me/app/node_modules/next/dist/server/next-server.js:827:34)
    at async Router.execute (/Users/me/app/node_modules/next/dist/server/router.js:222:32)
    at async DevServer.run (/Users/me/app/node_modules/next/dist/server/next-server.js:1135:29)
    at async DevServer.run (/Users/me/app/node_modules/next/dist/server/dev/next-dev-server.js:445:20)
    at async DevServer.handleRequest (/Users/me/app/node_modules/next/dist/server/next-server.js:325:20)
{ body: undefined }

By calling:

curl 'http://localhost:3000/api/token' -X POST  -H 'Accept: application/json' -H 'Content-Type: application/json' --data-raw '{"test":1}'

Expected Behavior

{ test: 1 } should be returned from await req.json()

To Reproduce

Create a basic middleware as outlined above and call it with the basic curl command.

@switz switz added the bug Issue was opened via the bug report template. label Dec 14, 2021
@balazsorban44 balazsorban44 added Middleware Related to Next.js Middleware. kind: bug and removed bug Issue was opened via the bug report template. labels Dec 15, 2021
@javivelasco
Copy link
Member

Closing this in favour of #30953

@github-actions
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 14, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Middleware Related to Next.js Middleware.
Projects
None yet
Development

No branches or pull requests

3 participants