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)
The simplest way to read a JSON file is to require it. Passing require() with the path to a JSON file will synchronously read and parse the data into a JavaScript object. const config = require("./config.
send(), sendStatus() and json() method in Node. js. The send() and json() functions are used for sending the response to the client directly from the server. The send() method will send the data in a string format, whereas the json() function will send the same in JSON format.
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); });
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With