'how to access data from simple link with api key through http request
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 = fetch(https://www.googleapis.com/blogger/v3/blogs/my-ID?key=Api_key);
q = l.json();
console.log(q.kind);
Solution 1:[1]
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
Solution 2:[2]
// 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,
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | mrt |
Solution 2 | James Oyanna |