Hello - I am trying to call an api using fetch but with no success. I have tried two methods and both have no results. I think the problem lies with the api key in the header and am not sure how to apply that.
Here is the api details:
curl -X ‘GET’ \
‘https://somewebsite.###/api/v1/activities?limit=50&offset=0’ \
-H ‘accept: application/json’ \
-H ‘apikey: asdasd-asdasdas-asdas-asas’
This is my first code attempt-
export function getAllActivities() {
const url = “https://somewebsite.###/api/v1/activities?limit=50&offset=0”;
const headers = {
"accept": "application/json",
"apikey": "asdasd-asdasdas-asdas-asas"
}
const request = {
"method": "get",
"headers": headers
}
return fetch(url, request)
.then(response => console.log(response.text()))
.catch(err => console.log(err));
}
The result returned was undefined object.
I then tried another way using the below code-
export function tryGetData () {
return fetch(‘https://somewebsite.###/api/v1/activities?limit=50&offset=0’, {
‘method’: ‘post’,
‘headers’: {
‘accept’: ‘application/json’,
‘apikey’: ‘asdasd-asdasdas-asdas-asas’
}
})
.then((httpResponse) => {
if (httpResponse.ok) {
return httpResponse.json();
} else {
return Promise.reject(‘Fetch did not succeed’);
}
})
.then((json) => {
return json.result;
})
.catch(err => console.log(err));
}
The result returned was “Fetch did not succeed”.
Thank you