TransWikia.com

Using request to return a json value

Stack Overflow Asked by GTA.sprx on January 26, 2021

I’m trying to use request to make a request to this api https://api.ipify.org/?format=json and receive back some json like this {"ip":"XX.XX.XXX.XX"} from there I want to parse it and let my function’s callback console.log the ip.

I’m just learning about callbacks and async so please give me any advice you can 🙂

const url = 'https://api.ipify.org/?format=json';

const getMyIP = function (callback) {
  request(url, (error, body, _response) => {
    body = JSON.parse(body);
    const ip = body["ip"];
    return ip;
  });
};


getMyIP((error, ip) => {
  if (!error) {
    console.log(ip);
  }
});

One Answer

fetch("https://api.ipify.org/?format=json")
.then(res => res.json())
.then(val => {console.log(val.ip);})
.catch(e => console.log(`Error - ${e}`))

Answered by yk1800 on January 26, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP