koajs / koa-body

koa body parser middleware
MIT License
950 stars 130 forks source link

Unexpected error when option.includeUnparsed is set and request body is empty #129

Closed SilentAntenna closed 5 years ago

SilentAntenna commented 5 years ago

Problem/Feature Request Summary

Environment Information

Current Behavior

If options.includeUnparsed is set, line 106/119 in index.js will always be executed:

ctx.request.body[symbolUnparsed] = body.raw;

which may trigger an exception when the request doesn't have a body and ctx.request.body[symbolUnparsed] is undefined.

Possible Solution

fix line 105/118 like this:

if (body.parsed && ! ctx.is('text')) {

MarkHerhold commented 5 years ago

Thanks for the report and sorry you hit an issue.

@zevisert, could you weigh in on this?

zevisert commented 5 years ago

Spot on report - I saw the title and realized right away it was that line, my bad. I guess I was going under the assumption that co-body was returning an empty object rather than undefined. I'll shoot you a PR later today.

MarkHerhold commented 5 years ago

Much appreciated @zevisert !

SilentAntenna commented 5 years ago

Many thanks. I forgot to close this issue, sorry.