am trying to fetch one public api in javascript code. in post man and in the console of the same site getting below response.
{
"timestamp": 1647320524,
"metal": "XAU",
"currency": "INR",
"exchange": "IDC",
"symbol": "FX_IDC:XAUINR",
"prev_close_price": 149199.3,
"open_price": 149459.8,
"low_price": 147829.3,
"high_price": 149531.3,
"open_time": 1647302400,
"price": 147796.4,
"ch": -1402.9,
"chp": -0.94,
"ask": 147806.4,
"bid": 147796.4,
"price_gram_24k": 4751.7646,
"price_gram_22k": 4355.7842,
"price_gram_21k": 4157.794,
"price_gram_20k": 3959.8038,
"price_gram_18k": 3563.8235
}
but when I try to hit the api via js getting http status 200 but no expected data. below is the js response
body: (...)
bodyUsed: false
headers: Headers {}
ok: true
redirected: false
status: 200
statusText: "OK"
type: "cors"
url: "https://www.goldapi.io/api/XAU/INR"
[[Prototype]]: Response
this is my JS code which I used to hit the api. I dont understand what the issue is.
var currency = 'INR';
var metal = 'XAU'; //Gold
function alertPrice(){
fetchGoldPrice(metal, currency)
// .then(response => alert(response.text()), response.text())
.then(result => {priceEl.innerHTML = JSON.stringify(result), console.log(result," result")})
.catch(error => {priceEl.innerHTML = JSON.stringify(error), console.log('error', error)});
}
function fetchGoldPrice(metal, currency){
const requestOptions = formHeaders();
return fetch(`https://www.goldapi.io/api/${metal}/${currency}`, requestOptions)
}
function formHeaders(){
const myHeaders = getHeaders();
var requestOptions = {
method: 'GET',
headers: {
"x-access-token": "my api key",
"Content-Type": "application/json"
}
};
return requestOptions;
}