GithubHelp home page GithubHelp logo

Comments (12)

tomas avatar tomas commented on July 19, 2024

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

from needle.

meenie avatar meenie commented on July 19, 2024

I'm getting a 200 response on both callbacks.

from needle.

tomas avatar tomas commented on July 19, 2024

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

from needle.

meenie avatar meenie commented on July 19, 2024

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

from needle.

tomas avatar tomas commented on July 19, 2024

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.

from needle.

tomas avatar tomas commented on July 19, 2024
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?

from needle.

yoniholmes avatar yoniholmes commented on July 19, 2024

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

from needle.

tomas avatar tomas commented on July 19, 2024

Hey Jonathan,

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

Cheers

from needle.

tomas avatar tomas commented on July 19, 2024

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

from needle.

tomas avatar tomas commented on July 19, 2024

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

from needle.

tomas avatar tomas commented on July 19, 2024

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.

from needle.

tomas avatar tomas commented on July 19, 2024

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

from needle.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.