lwsjs / local-web-server

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

Documentation update for 'lws-body-parser' #71

Closed nstansbury closed 5 years ago

nstansbury commented 7 years ago

Attempting to access a non-JSON request body

The docs here: https://github.com/lwsjs/local-web-server/wiki/How-to-access-the-body-of-an-incoming-request

require('lws-body-parser') but don't tell you how to use it

The docs here: https://github.com/lwsjs/local-web-server/wiki/Using-middleware#built-in-middleware-stack

Suggest the lws-body-parser is in the default stack. ws middleware-list confirms it is.

Unless the application type is application/json the ctx.request.body/ctx.request.rawBody etc is either undefined or {}

75lb commented 7 years ago

are you asking me to update the documentation? What type of request body are you trying to access?

nstansbury commented 7 years ago

The two I have tried so far are text/plain and application/jwt - the data is just a base64 encoded string.

Apologies, I should clarify I'm following the docs here: https://github.com/lwsjs/local-web-server/wiki/How-to-prototype-a-REST-API-using-Mock-Responses and just mocking some PUT requests

75lb commented 7 years ago

i will update the docs tonight with some examples, thanks.

If you think you've found a bug, please post a reproduction case. Otherwise, try running ws with the --verbose flag to inspect all traffic..

nstansbury commented 7 years ago

Some examples would be great thank you.

I'm pretty certain the error is in my code. I'm unclear as to whether we need to require the full require('koa'); app as per their docs here: https://github.com/koajs/bodyparser or if there's some heavy lifting done by lws-body-parser etc

nstansbury commented 7 years ago

The easiest option in the end was to revert to the application/json content-type and serialise & send the string inside an array - JSON.stringify([string]). In the handler I just had to call ctx.request.body[0];