koajs / koa-body

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

Cannot translate multiple/form-data correctly #103

Closed lzszone closed 6 years ago

lzszone commented 6 years ago

Problem/Feature Request Summary

Cannot translate multiple/form-data correctly when a form only contains some simple fields(not file)

Environment Information

Steps to Reproduce

Same as above.

Expected Behavior

ctx.request.body should be an Object and contains fields I added.

Possible Solution

Context (Environment)

This is most probably my fault... And hope you guys could give me some pieces of information. thanks

MarkHerhold commented 6 years ago

Hey @lzszone have you taken a look at the example here?

https://github.com/dlau/koa-body/blob/master/examples/koa-router.js#L92

You are correct that ctx.request.body should contain the fields you POSTed.

lzszone commented 6 years ago

@MarkHerhold Thank you. I tested the example you mentioned, and it results in the same, I just change the module entry.

My test tool is Postman Chrome app, and node version is the newest.

Here is what I got from Postman:

{
  "------WebKitFormBoundaryjsXkEhc2CYtWuiAH\r\nContent-Disposition: form-data; name": "\"username\"\r\n\r\nlzszone\r\n------WebKitFormBoundaryjsXkEhc2CYtWuiAH\r\nContent-Disposition: form-data; name=\"password\"\r\n\r\npwd\r\n------WebKitFormBoundaryjsXkEhc2CYtWuiAH\r\nContent-Disposition: form-data; name=\"anotherfield\"\r\n\r\nyep\r\n------WebKitFormBoundaryjsXkEhc2CYtWuiAH--\r\n"
}

Here is What I got from the console:

body { '------WebKitFormBoundaryjN1oIKCdtADfVj6p\r\nContent-Disposition: form-data; name':
   '"asdasdasd"\r\n\r\nasdasdasd\r\n------WebKitFormBoundaryjN1oIKCdtADfVj6p\r\nContent-Disposition: form-data; name="asdsa"\r\n\r\nasdasd\r\n------WebKitFormBoundaryjN1oIKCdtADfVj6p\r\nContent-Disposition: form-data; name="aassad"\r\n\r\nasdasd\r\n------WebKitFormBoundaryjN1oIKCdtADfVj6p--\r\n' }
body { '------WebKitFormBoundaryjsXkEhc2CYtWuiAH\r\nContent-Disposition: form-data; name':
   '"username"\r\n\r\nlzszone\r\n------WebKitFormBoundaryjsXkEhc2CYtWuiAH\r\nContent-Disposition: form-data; name="password"\r\n\r\npwd\r\n------WebKitFormBoundaryjsXkEhc2CYtWuiAH\r\nContent-Disposition: form-data; name="anotherfield"\r\n\r\nyep\r\n------WebKitFormBoundaryjsXkEhc2CYtWuiAH--\r\n' }

I think it's easy for you to reproduce... thank you! :D

MarkHerhold commented 6 years ago

Here's my attempt at reproducing your issue.

Sample request:

image

Produces these request headers:

accept:"*/*"
accept-encoding:"gzip, deflate"
cache-control:"no-cache"
connection:"keep-alive"
content-length:"258"
content-type:"multipart/form-data; boundary=--------------------------354715120723962812507950"
host:"localhost:4290"
postman-token:"6b085bb4-4cb8-4fea-83e2-882f6a78f74f"
user-agent:"PostmanRuntime/7.1.1"

Notice the content-type header is multipart/form-data, so you should set the multipart: true option on koa-body.

The parsed body correctly shows up as:

{a: "b", c: "d"}

If you are still seeing an issue with garbage in the parsed request body, please provide a curl command that demonstrates the issue.

lzszone commented 6 years ago

This is my fault... I forget to remove additional headers when posting, it replace content-type with application/x-www-form-urlencoded automatically... awkward:( Thanks a lot

MarkHerhold commented 6 years ago

No problem. Good luck with your app. :)