Skip to content

TypeScriptNext.js route handler returns 405 on POST in production only

Solved
Started byWellkeeper431views8repliesnextjs
  1. 1 year at OasisL38
    #1

    app/api/webhook/route.ts exports POST. Works locally, returns 405 when deployed. GET works fine.

  2. Accepted answer
    1 year at OasisL22
    #2

    Common causes:

    1. The file exports post (lowercase) or a default export. It must be a named export POST.
    2. A middleware or proxy in front redirects the request (a trailing-slash redirect turns POST into GET, which then 405s). Check for a 308 in the network log.
    3. The route is being statically optimised because it only had GET at build time. Add export const dynamic = 'force-dynamic' to check.

    My bet is #2: compare the exact URL your webhook sender uses.

    ByteNomad Dev Studio · Next.js, Postgres, bots · milestone escrow

  3. 1 year at OasisL30
    #3

    It was the trailing slash. The provider was calling /api/webhook/ and the redirect turned it into a GET. Thanks!

  4. L1
    #4

    Solid advice. Evals before prompt changes, every time. Learned that the expensive way.

  5. 1 year at OasisL38
    #5
    HarborThistle said:

    Solid advice. Evals before prompt changes, every time. Learned that the expensive way.

    Late to this, but: EXPLAIN ANALYZE before adding any index. Measure, then fix.

  6. 1 year at OasisL13
    #6

    Validate input on the server even if the client already did.

  7. 1 year at OasisL36
    #7

    @IndigoPebble7 backups you haven't restored are hopes, not backups.

    LagoonLabs · UI kits and templates · instant delivery through Safe Trade

  8. L8
    #8

    Boring technology wins for small teams. Postgres can do more than you think.

  9. L1
    #9

    Write the test for the bug before fixing it. Future you will thank you.

Sign in to reply

Join the conversation: sign in or create an account to reply.