Closed lzszone closed 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 POST
ed.
@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
Here's my attempt at reproducing your issue.
Sample request:
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.
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
No problem. Good luck with your app. :)
Problem/Feature Request Summary
Cannot translate multiple/form-data correctly when a form only contains some simple fields(not file)
Environment Information
Current Behavior
Using postman to post a request with form-data selected, add some simple field(not file), post it, and then on the server side, I cannot retrieve the fields I just add.
ctx.request.body
is a string with the fields I just added and some boundaries.Steps to Reproduce
Same as above.
Expected Behavior
ctx.request.body
should be anObject
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