Reputation: 6338
I am trying to get the facebook profile picture of the user logged into my application. Facebook's API states that http://graph.facebook.com/517267866/?fields=picture
returns the correct URL as a JSON object.
I want to get the URL to the picture out of my code. I tried the following but I am missing something here.
var url = 'http://graph.facebook.com/517267866/?fields=picture';
http.get(url, function(res) {
var fbResponse = JSON.parse(res)
console.log("Got response: " + fbResponse.picture);
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
Running this code results in the following:
undefined:1
^
SyntaxError: Unexpected token o
at Object.parse (native)
Upvotes: 94
Views: 194287
Reputation: 4365
Another solution is to user axios:
npm install axios
Code will be like:
const url = `${this.env.someMicroservice.address}/v1/my-end-point`;
const { data } = await axios.get<MyInterface>(url, {
auth: {
username: this.env.auth.user,
password: this.env.auth.pass
}
});
return data;
Upvotes: 3
Reputation: 12034
I'm using get-json very simple to use:
$ npm install get-json --save
Import get-json
var getJSON = require('get-json')
To do a GET
request you would do something like:
getJSON('http://api.listenparadise.org', function(error, response){
console.log(response);
})
Upvotes: 13
Reputation: 111434
JSON.parse
All of the answers here use JSON.parse()
in an unsafe way.
You should always put all calls to JSON.parse()
in a try/catch
block especially when you parse JSON coming from an external source, like you do here.
You can use request
to parse the JSON automatically which wasn't mentioned here in other answers. There is already an answer using request
module but it uses JSON.parse()
to manually parse JSON - which should always be run inside a try {} catch {}
block to handle errors of incorrect JSON or otherwise the entire app will crash. And incorrect JSON happens, trust me.
Other answers that use http
also use JSON.parse()
without checking for exceptions that can happen and crash your application.
Below I'll show few ways to handle it safely.
All examples use a public GitHub API so everyone can try that code safely.
request
Here's a working example with request
that automatically parses JSON:
'use strict';
var request = require('request');
var url = 'https://api.github.com/users/rsp';
request.get({
url: url,
json: true,
headers: {'User-Agent': 'request'}
}, (err, res, data) => {
if (err) {
console.log('Error:', err);
} else if (res.statusCode !== 200) {
console.log('Status:', res.statusCode);
} else {
// data is already parsed as JSON:
console.log(data.html_url);
}
});
http
and try/catch
This uses https
- just change https
to http
if you want HTTP connections:
'use strict';
var https = require('https');
var options = {
host: 'api.github.com',
path: '/users/rsp',
headers: {'User-Agent': 'request'}
};
https.get(options, function (res) {
var json = '';
res.on('data', function (chunk) {
json += chunk;
});
res.on('end', function () {
if (res.statusCode === 200) {
try {
var data = JSON.parse(json);
// data is available here:
console.log(data.html_url);
} catch (e) {
console.log('Error parsing JSON!');
}
} else {
console.log('Status:', res.statusCode);
}
});
}).on('error', function (err) {
console.log('Error:', err);
});
http
and tryjson
This example is similar to the above but uses the tryjson
module. (Disclaimer: I am the author of that module.)
'use strict';
var https = require('https');
var tryjson = require('tryjson');
var options = {
host: 'api.github.com',
path: '/users/rsp',
headers: {'User-Agent': 'request'}
};
https.get(options, function (res) {
var json = '';
res.on('data', function (chunk) {
json += chunk;
});
res.on('end', function () {
if (res.statusCode === 200) {
var data = tryjson.parse(json);
console.log(data ? data.html_url : 'Error parsing JSON!');
} else {
console.log('Status:', res.statusCode);
}
});
}).on('error', function (err) {
console.log('Error:', err);
});
The example that uses request
is the simplest. But if for some reason you don't want to use it then remember to always check the response code and to parse JSON safely.
Upvotes: 58
Reputation: 92579
Unirest library simplifies this a lot. If you want to use it, you have to install unirest
npm package. Then your code could look like this:
unirest.get("http://graph.facebook.com/517267866/?fields=picture")
.send()
.end(response=> {
if (response.ok) {
console.log("Got a response: ", response.body.picture)
} else {
console.log("Got an error: ", response.error)
}
})
Upvotes: 0
Reputation: 92579
I think that for simple HTTP requests like this it's better to use the request
module. You need to install it with npm (npm install request
) and then your code can look like this:
const request = require('request')
,url = 'http://graph.facebook.com/517267866/?fields=picture'
request(url, (error, response, body)=> {
if (!error && response.statusCode === 200) {
const fbResponse = JSON.parse(body)
console.log("Got a response: ", fbResponse.picture)
} else {
console.log("Got an error: ", error, ", status code: ", response.statusCode)
}
})
Upvotes: 19
Reputation: 14881
The res
argument in the http.get()
callback is not the body, but rather an http.ClientResponse object. You need to assemble the body:
var url = 'http://graph.facebook.com/517267866/?fields=picture';
http.get(url, function(res){
var body = '';
res.on('data', function(chunk){
body += chunk;
});
res.on('end', function(){
var fbResponse = JSON.parse(body);
console.log("Got a response: ", fbResponse.picture);
});
}).on('error', function(e){
console.log("Got an error: ", e);
});
Upvotes: 164