Hi all
I am working on an app that interfaces with a web API returning json. The response is gzipped and this seems to be causing issues as the fetch
function does not ungzip the content correctly.
Function to do the API call:
function apiFetchAuth(path, options, token) {
var url = encodeURI(ROOT_URL + path);
if(options === undefined) {
options = {};
}
// If a body is provided, serialize it as JSON and set the Content-Type header
options = Object.assign({}, options, {
body: JSON.stringify(options.body),
headers: Object.assign({}, options.headers, {
"Content-Type": "application/json; charset=UTF-8",
"Accept-Encoding" : "gzip",
})
});
// Fetch the resource and parse the response as JSON
return fetch(url, options)
.then(function(response) {
console.log(response.headers.get('content-encoding'));
return response.text().catch(function(err) {
console.log(`${err}`);
});
}).then(function(obj) {
console.log('result');
console.log(`${obj}`);
});
}
This logs that the content encoding received is gzip, but when trying to log the result the characters are unknown (<?>). Amending return response.text()
to return response.json()
returns this error:
SyntaxError: Unexpected token <?> in JSON at position 0
I have tried including numerous libraries in order to complete the API request. Solutions that work in browser or on cli using node do not work in Fuse.
Is there anything I am missing? What is the resolution to the above?