Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to get json response from rest api in node js

'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);
    }
});
Comment

how to get json response from rest api in node js

'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);
});
Comment

how to get json response from rest api in node js

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);
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: css select all links in div 
Javascript :: findoneandupdate mongoose 
Javascript :: modulus js 
Javascript :: Get the Last Items in an Array 
Javascript :: angular material datepicker range get value 
Javascript :: import npm dotenv package 
Javascript :: datatables and toggle not working 
Javascript :: javascript merge two array with spread operator 
Javascript :: javascript remove json element 
Javascript :: javascript apply 
Javascript :: js array as parameter 
Javascript :: Initialize Axios React Redux CRUD API calls 
Javascript :: ? operator in js 
Javascript :: nuxt import css 
Javascript :: jQuery DataTables Checkboxes 
Javascript :: vue 3 custom input component 
Javascript :: check last url in javascript 
Javascript :: discord js if no arguments 
Javascript :: js how to get max sub array sum 
Javascript :: javascript delete user input value in array 
Javascript :: fibonacci numbers 
Javascript :: express controller 
Javascript :: javascript number if .00 truncate 
Javascript :: how to check if email already exists in database using javascript 
Javascript :: js regex find 
Javascript :: sortable jquery 
Javascript :: mdn object assign 
Javascript :: luxon plus 
Javascript :: get node degree networkx 
Javascript :: moves zeroes 
ADD CONTENT
Topic
Content
Source link
Name
6+6 =