Hey guys,
I have a brand-new loopback project, and I'm trying to follow the getting started guide. Unfortunately, when I go to try and extend my API with a simple "greeting" method, the "msg" parameter is always undefined.
I've been fiddling with it, and it looks like the request body is completely empty:
module.exports = function (Offer) {
Offer.greet = function (message, cb) {
console.log(message); // Undefined if the "http" and "required" are removed from the argument below.
cb(null, 'Hello, ' + message);
}
Offer.remoteMethod(
'greet',
{
accepts: {
arg: 'message',
type: 'string',
required: true,
http: function (ctx) {
console.log(ctx.req.body); // Completely empty. Produces "{}" in the console.
return 'World!';
}
},
returns: { arg: 'result', type: 'string' },
http: { verb: 'POST' }
}
);
}
This only seems to be an issue with POST requests. GET requests work just fine. If I leave out the http from the argument, I get a 400: "message is a required arg" error. If I remove the "required", the message is simply undefined.
I'm not sure if there's something I'm missing, or if there's a bug here, but any help you could offer would be greatly appreciated!
Thanks.
After a bit more poking around, this might be related more to the API explorer than loopback itself. Using Postman (a chrome extension for testing REST APIs), I was able to successfully call the remote greet method after ensuring that the data was sent as either x-www-form-urlencoded, or JSON with the appropriate Content-Type. It looks like the loopback-explorer is, for whatever reason, not sending the request payload at all. I've attached screenshots



.
I guess that weβre not telling Swagger that application/x-www-form-urlencoded or multipart/form-data is supported.
You should be able to customize it by adding βconsumesβ to the βhttpβ settings. Maybe it also makes sense for us to add them as default types in addition to application/json.
Thanks,
Raymond Feng
Co-Founder and Architect @ StrongLoop, Inc.
StrongLoop makes it easy to develop APIs in Node, plus get DevOps capabilities like monitoring, debugging and clustering.
On Aug 5, 2014, at 1:31 PM, Eric Abruzzese [email protected] wrote:
After a bit more poking around, this might be related more to the API explorer than loopback itself. Using Postman (a chrome extension for testing REST APIs), I was able to successfully call the remote greet method after ensuring that the data was sent as either x-www-form-urlencoded, or JSON with the appropriate Content-Type. It looks like the loopback-explorer is, for whatever reason, not sending the request payload at all. I've attached screenshots
.
β
Reply to this email directly or view it on GitHub.
This is a bug. Thanks for the detailed report. I think the fix involves handling POST correctly in the explorer UI.
@ritch I verified this, there is nothing in the POST body. Reassigning to you for bug fix.
@superkhau I cannot reproduce this...
Here is my model.js:
module.exports = function(Offer) {
Offer.greet = function (message, cb) {
console.log(message); // Undefined if the "http" and "required" are removed from the argument below.
cb(null, 'Hello, ' + message);
}
Offer.remoteMethod(
'greet',
{
accepts: {
arg: 'message',
type: 'string'
},
returns: { arg: 'result', type: 'string' },
http: { verb: 'POST' }
}
);
};
And here is what I get in the explorer:

@ritch I checked via the inspector in Firefox, not explorer. Try inspecting for body values via Chrome inspector and see if you get anything.
Here is what I get:

Looks fine to me...
@eabruzzese @superkhau
I'm assuming this has been fixed. If you guys have steps to repro or any other related issue please re-open this.
Im having that problem, req.body or req.params is always undefined
this is how Im using it
Participant.remoteMethod(
'register',
{
http: {path: '/register', verb: 'post'},
accepts: [
{arg: 'req', type: 'object', 'http': {source: 'req'}},
{arg: 'res', type: 'object', 'http': {source: 'res'}},
Same here
Yup, also happening for me on a remoteMethod using delete as the HTTP-Verb.
Here is a screenshot of the request and as you can see there is no formData appended to my request.
Works fine if i just use the API, so it looks like the swagger explorer is missing this.

So propably this issue should be reopened /cc @ritch
Hey @martinjuhasz, have you found a solution for this bug?. Same issue using the verb DELETE.
No sorry, i don't work anymore on that project and i'm not sure if this is solved. I guess the swagger implementation has problems with uncommon http-types like DELETE + body data.
Same problem here with a DELETE remote method.
βββ [email protected]
βββ [email protected]
βββ [email protected]
βββ [email protected]
βββ [email protected]
βββ [email protected]
βββ [email protected]
The issue is marked as closed. Is there any fix available?