vercel / serve-handler

The foundation of `serve`

Home Page:https://npmjs.com/serve-handler

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Request losts when POSTing redirected url

jonathanffon opened this issue · comments

I hosted a web on port 3000 and served my API on port 3080. All POSTs of "/api/" were redirected to "localhost:3080/". In serve.json, I have:

"redirects": [
    {
      "source": "/api/:obj",
      "destination": "http://localhost\\:3080/:obj"
    }
  ]

It was OK for GET method, but for POST method, req.body was empty when it was redirected to port 3080.
Here is POST results issued by curl. The difference was caused by request lose:

➜  curl -L -H 'Content-Type:application/json' -X POST http://localhost:3000/api/timers -d '{"title":"test1"}'
{
  "id": 12
}%
➜  curl -L -H 'Content-Type:application/json' -X POST http://localhost:3080/timers -d '{"title":"test2"}'
{
  "title": "test2",
  "id": 13
}%

Any fix to this problem ?

try this:

{
  "redirects": [
    {
      "source": "/api/:obj",
      "destination": "http://localhost\\:3080/:obj",
      "type": 307
    }
  ]
}

references:

try this:

{
  "redirects": [
    {
      "source": "/api/:obj",
      "destination": "http://localhost\\:3080/:obj",
      "type": 307
    }
  ]
}

references:

Thank you @warren-bank . That's great. I have tried 307 and 308. Both work for me.