Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POST not working? #6

Closed
TobyEalden opened this issue Aug 8, 2013 · 9 comments
Closed

POST not working? #6

TobyEalden opened this issue Aug 8, 2013 · 9 comments

Comments

@TobyEalden
Copy link

Hi

I'm having trouble getting POST methods to work - the request arrives at my API, but the req.body element is an empty object (even though I have specified args.data). GET works fine.

I tried with both direct and registered methods.

My API is implemented using express and is currently running on localhost:5000. The API works if I use an HTML form to post...

Any ideas what I might be missing?

Thanks,

Toby

@aacerox
Copy link
Owner

aacerox commented Aug 8, 2013

Hi

don't know I'll take a look.

Thanks.

@aacerox
Copy link
Owner

aacerox commented Aug 8, 2013

Hi again

Could you please send me the code (or similar code ...) your using with the POST method?

thanks in advance.

@jetume
Copy link
Contributor

jetume commented Aug 8, 2013

Please take a look at my pull request, I am guessing since Content-Length header wasn't being sent, express might be thinking that the request came with an empty body

#8

@aacerox
Copy link
Owner

aacerox commented Aug 9, 2013

fix it with pull request #8

Thanks.

@aacerox aacerox closed this as completed Aug 9, 2013
@TobyEalden
Copy link
Author

Still not working for me. See this https://gist.github.com/TobyEalden/6192116

Also, the recent pull request introduced a bug whereby args.headers is mandatory - if this line https://gist.github.com/TobyEalden/6192116#file-test-js-L7 is missing there is a crash.

@aacerox
Copy link
Owner

aacerox commented Aug 9, 2013

yep, you're Right I've just pushed the fix for the headers issue. I'm going to test your code and see if I can find what happends

Thanks

@aacerox aacerox reopened this Aug 9, 2013
@aacerox
Copy link
Owner

aacerox commented Aug 9, 2013

Hi

I've been trying with my own server (simple node.js server without using "express.js") and everything works fine. please see https://gist.github.com/aacerox/f2fdf78e651804e5b004.

It seems to be something in the way express manage requests. Maybe you can use a node debugger like https://github.com/node-inspector/node-inspector to debug what's "express.js" doing with requests.

by the way you can enable console debug on node-rest-client by setting an environtment variable DEBUG like:

windows -> set DEBUG=true
linux -> export DEBUG=true

I'll try to see something by my self.

@aacerox
Copy link
Owner

aacerox commented Aug 9, 2013

I've found it!!!

you must set Content-type:application/json in client params headers:

var restClient = require("../../lib/node-rest-client").Client;
var appgAPIClient = new restClient();
var apiURLBase = "http://localhost:5000";

var args = {
  data: { test: "hello" },
  headers:{"Content-Type": "application/json"} 
};

appgAPIClient.post(apiURLBase + "/postTest", args, function(data,response) {
  if (data.hasOwnProperty("ok") && data.ok === true) {
    console.log("success");
  } else {
    console.log("failure");
  }
});

Also I've a bug in my last commit with header initialization, that I've already fix.

@aacerox aacerox closed this as completed Aug 9, 2013
@TobyEalden
Copy link
Author

Great thanks - I can confirm that works for me too.

BTW strictly speaking, https://github.com/aacerox/node-rest-client/blob/master/lib/node-rest-client.js#L149 should be

if (typeof options.headers === "undefined")

:)

Thanks for your help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants