loopback icon indicating copy to clipboard operation
loopback copied to clipboard

remoteMethod() has no request parameters when POSTed to.

Open eabruzzese opened this issue 12 years ago • 14 comments

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.

eabruzzese avatar Aug 05 '14 17:08 eabruzzese

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 screen shot 2014-08-05 at 4 05 25 pm screen shot 2014-08-05 at 4 04 05 pm screen shot 2014-08-05 at 4 03 32 pm

.

eabruzzese avatar Aug 05 '14 20:08 eabruzzese

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.

raymondfeng avatar Aug 05 '14 20:08 raymondfeng

This is a bug. Thanks for the detailed report. I think the fix involves handling POST correctly in the explorer UI.

ritch avatar Aug 08 '14 00:08 ritch

@ritch I verified this, there is nothing in the POST body. Reassigning to you for bug fix.

superkhau avatar Dec 22 '14 22:12 superkhau

@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:

screen shot 2015-01-09 at 11 05 28 am

ritch avatar Jan 09 '15 19:01 ritch

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

superkhau avatar Jan 09 '15 19:01 superkhau

Here is what I get:

screen shot 2015-01-09 at 12 15 44 pm

Looks fine to me...

ritch avatar Jan 09 '15 20:01 ritch

@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.

ritch avatar Jan 09 '15 20:01 ritch

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'}},

gholias avatar Sep 17 '15 16:09 gholias

Same here

ashdns avatar Oct 30 '15 03:10 ashdns

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.

ghissue1

So propably this issue should be reopened /cc @ritch

martinjuhasz avatar Feb 09 '16 09:02 martinjuhasz

Hey @martinjuhasz, have you found a solution for this bug?. Same issue using the verb DELETE.

carlosroec avatar Jan 27 '17 03:01 carlosroec

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.

martinjuhasz avatar Jan 27 '17 09:01 martinjuhasz

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?

jmereaux avatar Mar 08 '17 15:03 jmereaux