Home > OS >  how to access data from simple link with api key through http request
how to access data from simple link with api key through http request

Time:03-03

I have this simple link https://www.googleapis.com/blogger/v3/blogs/my-ID?key=Api_key

It works fine and give me this

{
  "kind": "blogger#blog",
  "id": "##############",
  "name": "blog-blogger",
  "description": "",
  "published": "2022-02-08T08:38:50-08:00",
  "updated": "2022-02-22T16:52:43-08:00",
  "url": "http://original-1-1.blogspot.com/",
  "selfLink":
  "posts": {
    "totalItems": 1
  },
  "pages": {
    "totalItems": 0
  },
  "locale": {
    "language": "en",
    "country": "?",
    "variant": ""
  }
}

now how i fetch above data using javascript

like

l = featch(https://www.googleapis.com/blogger/v3/blogs/my-ID?key=Api_key);
q = l.json();
console.log(q.kind);

CodePudding user response:

I am working on something similar this is the code I am using.

var request = new XMLHttpRequest()

// Open a new connection, using the GET request on the URL endpoint
request.open('GET', `https://www.googleapis.com/blogger/v3/blogs/my-ID?key=Api_key`, true)

request.onload = function () {
  // Begin accessing JSON data here
  var data = JSON.parse(this.response)
  console.log(data) 
}

Another option is using fetch like you suggested

function getData() {
  const response = await fetch('https://www.googleapis.com/blogger/v3/blogs/my-ID?key=Api_key')
  const data = await response.json()
}

This is the website I got my information from: https://www.taniarascia.com/how-to-connect-to-an-api-with-javascript/

Hope this answers your question!

mrt

CodePudding user response:

This will get you kind

const xhttp = new XMLHttpRequest();
xhttp.onload = function() {
    let responseJSON = JSON.parse(this.responseText);
        console.log(responseJSON.kind);
}
xhttp.open("GET", "https://www.googleapis.com/blogger/v3/blogs/my-ID?key=Api_key", true);
xhttp.send();

CodePudding user response:

// declare a variable to hold your API url like this const url = " https://www.googleapis.com/blogger/v3/blogs/my-ID?key=Api_key"

 // the spelling of your fetch is incorrect. You can use this

fetch(url) .then(response => response.json()) .then(data => console.log(data));

// this should work

// Using your API key Ensure you pass your API key into the REST API call as a query parameter by replacing API_KEY with your API key,

  • Related