lwsjs / local-web-server

A lean, modular web server for rapid full-stack development.
MIT License
1.21k stars 85 forks source link

request.body is undefined in POST and PUT requests #38

Closed 4O4 closed 8 years ago

4O4 commented 8 years ago

As the title says. I thought that maybe I am doing something wrong, but then I took the examples from repository (https://github.com/75lb/local-web-server/tree/master/example/mock) and the same problem occurs. PUT request to /users/1 is in fact removing the user, because there is no ctx.request.body and POST request to /users adds object with just "id" property to the users array. It's the most useful feature of mocks for me and sadly it is not working.

I've googled a bit and the problem seems to be related to Koa bodyparser. Tried to fix it myself, but I don't have enough knowledge about Koa to understand what exactly is going wrong and why.

75lb commented 8 years ago

you need to add a Content-Type: application/json header to the PUT request, to tell the server what format the data is in.. launch the mock example you linked to then run these two commands, you'll see the user record was updated:

$ curl http://127.0.0.1:8000/users/1 --header 'Content-Type: application/json' -X PUT --data-raw '{ "id": 1, "name": "cake" }'  && echo
$ curl http://127.0.0.1:8000/users/1  && echo
75lb commented 8 years ago

i need to improve the documentation.. let me know if you have any more issues.

4O4 commented 8 years ago

Well, yesterday I was testing with this header... but I double checked now and it works indeed. Must have done a typo in content-type or something. Thank you for your help!