Token Based Rest Api in JavaScript

Thanks to @mattlongfield for his python example. I have waited a long time for someone to help me with this. I really appreciate it, Matt.

Here is my working JavaScript Rest Api code (apiKey, apiSecret and url modified, so write your own values).

const headers = new Headers();
const apiKey = "0b25049e1c3fe6a"
const apiSecret = "a2f0f3225a5a8a7"
const url = "http://286.145.208.028/api/resource/Item"
const token = "token " + apiKey + ":" + apiSecret

headers.append('Authorization', token);
headers.append('cache-control', 'no-cache');

const init = {
	method: 'GET',
	headers
}

fetch(url, init)
.then((response) => {
	return response.text(); // or .json() or .blob() ...
})
.then((text) => {
	console.log(text) // text is the response body
})
.catch((e) => {
	console.log(e) // error in e.message
});
6 Likes