Comments (12)
That's weird. Are you getting a redirect or a regular 200 response?
from needle.
I'm getting a 200 response on both callbacks.
from needle.
What's in the response body? Can you share the URL you're making the request to?
from needle.
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.
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.
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.
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.
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.
Shoot I totally forgot about this! Sorry Jonathan, I'm on it right now!
from needle.
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.
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.
I'm closing it down for the time being. Feel free to reopen it if the problem persists.
from needle.
Related Issues (20)
- Very infrequent TypeError: iconv.encodingExists is not a function
- Tunnelling doesn't work in v3.1.0 HOT 12
- Proposal: Supporting User-Defined "Follow If" Conditions
- Piping needle request to Express response won't set the correct headers
- Multiple Files Upload Renames Input Field
- Uncaught asynchronous error
- Remote end closed socket abruptly errors HOT 2
- a ':' in nonce breaks digest authentication HOT 1
- Digest MD5 not working with Geovision IP Camera
- Uncaught ECONNRESET exception
- needle.get cannot guaranty getting the http stream HOT 2
- FEATURE: Retry HOT 1
- follow_set_cookies -> not working!!!
- Bug in 3.3.0: accidental creation of `global.signal`, gets caught in strict UTs
- README recommends mikeal's request HOT 3
- Multipart uploads strips date values in the request body HOT 2
- Doesn't work in Cloudflare Workers HOT 1
- Sorry, I was about to post this issue to a different repo, don't even know how it happened that I posted it here
- `get` with redirect leads to ~30sec hang HOT 7
- Invalid protocol causes uncatchable error in node:http(s)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from needle.