admin管理员组文章数量:1134576
I am trying to get the facebook profile picture of the user logged into my application. Facebook's API states that /?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 = '/?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)
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)
Share
Improve this question
edited May 21, 2016 at 13:00
Michał Perłakowski
92.4k30 gold badges163 silver badges185 bronze badges
asked Aug 6, 2012 at 10:20
SvenSven
6,33824 gold badges77 silver badges116 bronze badges
0
6 Answers
Reset to default 164The 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);
});
Problems with other answers:
- unsafe
JSON.parse
- no response code checking
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.
Example with 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);
}
});
Example with 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);
});
Example with 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);
});
Summary
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.
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)
}
})
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);
})
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;
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)
}
})
本文标签: javascriptCalling a JSON API with NodejsStack Overflow
版权声明:本文标题:javascript - Calling a JSON API with Node.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736834449a1954836.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论