POST request from middleware fails
See original GitHub issueVerify 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
-
Create a new Next.js project, eg
pnpm create next-app --ts
. -
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);
}
- Visit http://localhost:3000
You will now see the red warning box.
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:
- Created a year ago
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
is there a solution yet?
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.