How does LB parse the multipart/form-data fields in POST routes?
I'm testing with a 3rd-party who unfortunately had a test page that was sending GET requests which worked. Their actual webhooks, however, are sent as POST. I changed the remoteMethod http verb param to 'post' (although docs say it's default anyway) but the app cannot seem to capture the field values no matter what config I try.
MyModel.myMethod = function(field1, field2, field3, cb) {
var ok = dispatcher.dispatch('mymethod', dispatcher.IN, field1, field2, field3);
cb(ok ? null : "Error", ok ? "ok": "failed");
};
MyModel.remoteMethod('myMethod', {
description: "This method will handle incoming requests",
http: {verb: 'post', path: '/mymethod/in'},
accepts: [
{arg: 'field1', type: 'string', required: false, description: '...'},
{arg: 'field2', type: 'string', required: false, description: '...'},
{arg: 'field3', type: 'string', required: false, description: '...'}
],
returns: {arg: 'status', type: 'object', description: 'The response containing the status'}
});
The sender is sending the following (using sniffer to capture request coming in):
POST /v1/mymodel/mymethod/in HTTP/1.1
Host: my-host-name.com
Accept: */*
Content-Length: 1768
Expect: 100-continue
Content-Type: multipart/form-data; boundary=----------------------------d20fbfa47735
HTTP/1.1 100 Continue
------------------------------d20fbfa47735
Content-Disposition: form-data; name="field1"
value1
------------------------------d20fbfa47735
Content-Disposition: form-data; name="field2"
value2
------------------------------d20fbfa47735
Content-Disposition: form-data; name="field3"
value3
------------------------------d20fbfa47735
The app does not seem to recognize the incoming args and assign values and pass off to lib JS object. I've tried adding the http: { parse: 'form' } and body after the type and even tried adding body-parser to the middleware.json file in parse{} section but unsure what I'm doing wrong. I appreciate any help or working example of remoteMethod accepting multipart/form-data fields.
Is this SO article relevant and I need to add body-parser to package.json (although I thought was already part of LB) and then update middleware and import body parser in server/server.js file or is it built in already? I appreciate any working example with multipart/form-data fields.
EDIT: it appears body-parser does not handle multipart so if there is example using busboy, multiparty, or formiddable and what is recommended I appreciate.
UPDATE: I followed the steps in the SO article adding BOTH body-parser and multer modules, updating middleware.json and updating server.js and the code above now works. I hope this helps others.
SOLUTION: http://stackoverflow.com/questions/28523782/how-can-i-use-body-parser-with-loopback (see Ben Carlson's answer)
Thanks Mike! Glad you were able to get it working!
-Ben
I have done all that but mine still not working .I mean it really seems like the solution to this problem but don't know why mine won't work .I am sending data from front -end with angular ng-file-upload module like this
**```
Upload.upload(
url:urlto -my -remoteMethod,
data:{file:$scope.file,OtherInfo:$scope.otherInfo}
)
**
and my req.body despite all the multer and body-parser that i have done just as you did my req.body still returns nothing back
I created a new, updated answer: https://stackoverflow.com/a/52060729/605586
Most helpful comment
UPDATE: I followed the steps in the SO article adding BOTH body-parser and multer modules, updating middleware.json and updating server.js and the code above now works. I hope this helps others.
SOLUTION: http://stackoverflow.com/questions/28523782/how-can-i-use-body-parser-with-loopback (see Ben Carlson's answer)