serverless-next.js icon indicating copy to clipboard operation
serverless-next.js copied to clipboard

Method not allowed in pages

Open CervarlCG opened this issue 3 years ago • 1 comments

Issue Summary

Making a request to any page URL with the post method return an 503 error Method not allowed

Actual behavior

Actually, when a page is visited with a post method a error is returned, i know that it can be resolved changing the method to GET but some payment processor like paypal return to the page with a POST method so when a users end the payment and return to our website then receive a error

Expected behavior

Page is render like a GET Method

Steps to reproduce

Deploy any website and try to make a POST request to any page

Screenshots/Code/Configuration/Logs

image

Versions

  • OS/Environment: AWS Lambda
  • @sls-next/serverless-component version:
  • Next.js version:

Additional context

Checklist

  • [x] You have reviewed the README and FAQs, which answers several common questions.
  • [ ] You have reviewed our DEBUGGING wiki and have tried your best to include complete information and reproduction steps (including your configuration) as is possible. As there is only one maintainer (who maintains this in his free time) and thus very limited resources, if you have time, please try to debug the issue a bit yourself if possible.
  • [x] You have first tried using the most recent latest or alpha @sls-next/serverless-component release version, which may have already fixed your issue or implemented the feature you are trying to use. Note that the old serverless-next.js component and the serverless-next.js plugin are deprecated and no longer maintained.

CervarlCG avatar Feb 16 '22 15:02 CervarlCG

I got the exact same issue and the feature I was implementing is also a third-party payment service that uses form post for returning data and I have to redirect to a page to display the result for users.

The normal route (pages) in nextjs can't handle post requests, but API routes (pages/api) can.

So, I tried res.redirect(url) in an API route, but still not worked, and it took me a day to figure out what I encountered. 🤔

There is a status param that you can pass to res.redirect method, and the default is 307 https://nextjs.org/docs/api-routes/response-helpers

Then, I checked https://github.com/serverless-nextjs/serverless-next.js/issues/2021. It says:

if you did a POST to your API then the redirect will also POST to the redirect. In this case the origin S3 does not allow POST so it will fail. I think you can use 303 status to change to a GET

I used res.redirect(302, url) in the end, and it worked, too.

gong-hao avatar Mar 05 '22 07:03 gong-hao