needle.post() calling callback twice #44

Closed
meenie opened this Issue Dec 9, 2013 · 12 comments

Projects

None yet

3 participants

@meenie
meenie commented Dec 9, 2013

I'm using needle.post() and passing in a callback function. The callback function is getting called twice and the body on the first time is a javascript object (as expected) and the second time the body is a JSON string. Using the latest version of needle.

@tomas
Owner
tomas commented Dec 9, 2013

That's weird. Are you getting a redirect or a regular 200 response?

@meenie
meenie commented Dec 9, 2013

I'm getting a 200 response on both callbacks.

@tomas
Owner
tomas commented Dec 9, 2013

What's in the response body? Can you share the URL you're making the request to?

@meenie
meenie commented Dec 9, 2013

Sorry I can't as it's internal. But the body on the first callback is a json object. And the second it's a string of JSON.
So first is:

{foo: "bar"}

And the second is:

'{"foo":"bar"}'
@tomas
Owner
tomas commented Dec 9, 2013

Ok, I'll try to reproduce in a while and let you know what I find. My guess is that it has to do with the JSON parsing, so you might want to try passing parse: false in the options to see what happens.

@tomas
Owner
tomas commented Dec 10, 2013
var http   = require('http'),
    needle = require('needle');

var port = 4000;

http.createServer(function(req, res) {

  console.log('Got request');
  console.log(req.headers);

  res.writeHeader(200, {'Content-Type': 'application/json'});
  res.end(JSON.stringify({ foo: 'bar' }));

}).listen(port);

needle.get('localhost:' + port, function(err, resp) {
  console.log('Got response');
  console.log(err || resp.body);
});

setTimeout(function() {
  process.exit();
}, 2000);

This example works OK for me. Is there something different on your setup?

@yoniholmes

I'm encountering the same problem, but with needle.get(). Am using needle 0.6.3.

When calling needle.get(), my callback function is called twice. I'm also sending a custom header, but wouldn't know if that's affecting things or not. The request is being made over HTTPS.

The first callback has no error (ie. the error parameter is null), and the body is a JSON object as I expect.

The second callback's error parameter is a type error: [TypeError: object is not a function], and the body is the same as the first callback, although it's a string not a JSON object.

I can write some code to work around this, but wanted to report it because it feels like a bug?

Update: Note that setting the option parse: false makes the second callback with the error go away (ie. the callback gets called once only) although the body is no longer utf8 decoded, so the body that's returned is just a raw data buffer instead of being either a string or a JSON object. decode: true or decode: false has no effect.

Second update: Note that I'm sending an 'Authorization' header without credentials in the options, as per the requirements of the API I'm using. I saw in your source you might be treating those requests specially...

Cheers, Jonathan

@tomas
Owner
tomas commented Feb 22, 2014

Hey Jonathan,

Thanks for the report and the updates. I'll take a look later today and let you know what I find.

Cheers

@tomas
Owner
tomas commented Mar 5, 2014

Shoot I totally forgot about this! Sorry Jonathan, I'm on it right now!

@tomas
Owner
tomas commented Mar 5, 2014

Jonathan, two questions:

a) What other libraries have you loaded on your project? (To see if there's anything that might have overwritten or modified the behaviour of one of the core JS functions).
b) Would you mind printing out the headers that come in the first and/or second requests? console.log(resp.headers).

@tomas
Owner
tomas commented Mar 5, 2014

Ok I just pushed an updated version that (hopefully) gets the job done. My guess is that something else in your code is provoking that try / catch(e) to capture and callback the error, since the try / catch was wrapping the whole callback, not just the JSON.parse(data) call.

@tomas
Owner
tomas commented Mar 5, 2014

I'm closing it down for the time being. Feel free to reopen it if the problem persists.

@tomas tomas closed this Mar 5, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment