http-proxy-middleware can't handle POST request in next.js #11036
-
I want to build middle tier server with next.js, so it must proxy all request with To achieve this, I add file import { createProxyMiddleware } from "http-proxy-middleware";
export default createProxyMiddleware({
target: "http://yuanjin.tech:5100", //the data server
changeOrigin: true
}); After testing, all get requests works well, but all POST requests has problems # get request workds well
GET http://localhost:3000/api/movie # post request has problems
POST http://localhost:3000/api/user/login
BODY loginId=xxx&loginPwd=xxx
# console info
API resolved without sending a response for /api/user/login, this may result in stalled requests. When this error occurs, my data server does not actually receive any requests Then i have found any request with body has same problem, even GET request I don't know why the proxy request can't reach the data server, did i miss something? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 11 replies
-
I assume it's related to automatic body parsing: You can disable it using the config options outlined here: https://nextjs.org/docs/api-routes/api-middlewares#custom-config |
Beta Was this translation helpful? Give feedback.
-
Hello, For instance here is my
Is it something doable ? I I should go with the custom server ? |
Beta Was this translation helpful? Give feedback.
-
@alex-rhodes, without any other info, I would say to wait for your server api call to complete. Convert the function that makes the api call into an async function and prepend the await keyword to the api call invocation or use the old promise api to wait for the call to finish. After that, you will get your data. Then you might wind up with the "API resolved without sending a response..." message in the console if you're using the http-proxy-middleware. That is just because the creators of that middleware have not addressed that particular issue - it is an annoying cosmetic problem at best. You can chose to ignore it for now. The middleware itself does the actual job it was created to do rather well. |
Beta Was this translation helpful? Give feedback.
-
Hey all, |
Beta Was this translation helpful? Give feedback.
I assume it's related to automatic body parsing:
You can disable it using the config options outlined here: https://nextjs.org/docs/api-routes/api-middlewares#custom-config