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.

POST request from middleware fails

See original GitHub issue

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 21.5.0: Tue Apr 26 21:08:37 PDT 2022; root:xnu-8020.121.3~4/RELEASE_ARM64_T6000
Binaries:
  Node: 17.4.0
  npm: 8.3.1
  Yarn: 1.22.19
  pnpm: 7.4.0
Relevant packages:
  next: 12.2.4-canary.2
  eslint-config-next: 12.2.3
  react: 18.2.0
  react-dom: 18.2.0

What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

Describe the Bug

Fetch requests made from middleware fail but only if the request is a POST request with a request body.

Expected Behavior

The middleware should be able to make POST requests.

Link to reproduction

https://github.com/dferber90/nextjs-fetch-bug-reproduction

To Reproduce

  1. Create a new Next.js project, eg pnpm create next-app --ts.

  2. Add a middleware.ts file at the root with:

// middleware.ts
export const config = { matcher: "/" };

export async function middleware() {
  const text = await fetch("https://example.com", {
    method: "POST",
    body: "",
  }).catch((error) => {
    console.error(error);
    throw error;
  });

  console.log(text);
}
  1. Visit http://localhost:3000

You will now see the red warning box.

image

Additionally, the middleware will have logged the error to the terminal:

// from: console.error(error)
{ [TypeError: fetch failed]
  cause: [TypeError: define_process_default.nextTick is not a function] }

// from: throw error
error - Error [TypeError]: fetch failed
    at Object.processResponse (evalmachine.<anonymous>:3033:34)
    at <unknown> (evalmachine.<anonymous>:3366:42)
    at <unknown> (node:internal/process/task_queues:141:7)
    at AsyncResource.runInAsyncScope (node:async_hooks:201:9)
    at AsyncResource.runMicrotask (node:internal/process/task_queues:138:8)
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  middleware: true
}

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
Charlie-007-starcommented, Aug 2, 2022

is there a solution yet?

0reactions
github-actions[bot]commented, Sep 1, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What would be the response if a middleware fails in a REST ...
If your middleware did not throw an exception, and did not call next(), and did not respond, the request would hang.
Read more >
Code after middleware failure still running - Laracasts
in my main controller I'm running some middleware in the __construct, as below public function __construct() { $this->middleware('checkToken'); $this->user ...
Read more >
Handle GET and POST Request in Express - CodeForGeek
Express requires an additional middleware module to extract incoming data of a POST request. This middleware is called 'body-parser.
Read more >
API Reference - Express 4.x
Returns middleware that only parses JSON and only looks at requests where the Content-Type header matches the type option. This parser accepts any...
Read more >
Middleware | ZIO-HTTP - GitHub Pages
The polluted code violates the principle of "Separation of concerns"​ · Basic Auth · Request logging · Response logging · Timeout and retry....
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