Next-i18next: fetching nextjs api routes does not work for POST

Created on 14 Jan 2020  路  10Comments  路  Source: isaachinman/next-i18next

Describe the bug

I'm trying to fetch data from my nextjs api route with POST. The body I am posting is not present when executing the api route handler.

Occurs in next-i18next version

3.0.1

Steps to reproduce

clone https://github.com/Naxos84/next-i18next-demo.git
run npm i and npm run dev
open http://localhost:3000
inspect console(s)

Expected behaviour

the object from the requesthandler of api/demo should be returned.

OS (please complete the following information)

  • Device: [Windows 10]
  • Browser: [Firefox 72.0.1 64bit]

Additional context

I can reproduce that behaviour with Postman.
When posting to /api/demo I get the exact same error.
When getting to /api/demo everything works fine but I can't "GET" with a body from my browser ;)

I am not an expert.... so maybe I'm just doing something wrong?

Most helpful comment

I found the issue.

I had to add server.post("*", (req, res) => handle(req, res)); to server/index.ts.

maybe this should be mentioned in the documentation!?

All 10 comments

The repository is empty.

Sorry. Didn't pay enough attention. Fixed it.

fetching nextjs api routes does not work for POST

I can't "GET" with a body from my browser

So which is it? What unexpected behaviour are you reporting?

The unexpected behaviour is:
When I'm "POST"ing a body with my api-route call (/pages/index line 24) that this body (in my example code the "locale") is not present in my request-handler.

Because the "locale" is not present I'm sending a "400" in my request-handler (/pages/api/demo line 7)

That's because there was a redirect via localeSubpaths, because /api/ was not previously in ignoreRoutes. I've changed this and released it with v4.0.0.

Thank you very much for the quick response and quick release.

Do I have to specify my api-routes manually inside server/index.ts?

Cause when I'm trying with [email protected] (see my updated repo) I get a 404.

My requesthandler doesn't do anything and my problem is even worse than before because I don't get any request (in my request-handler).

I found the issue.

I had to add server.post("*", (req, res) => handle(req, res)); to server/index.ts.

maybe this should be mentioned in the documentation!?

If you're getting a 404 (and not a redirect) on your api routes, I doubt that has anything to do with next-i18next. You'll need to enquire on the NextJs side of things as to how the new api dir can/should interact with a custom server.

Glad you found it. That doesn't belong in next-i18next documentation as that's just generic to NextJs.

Was this page helpful?
0 / 5 - 0 ratings