I made an API call to an endpoint and it returns this:
const test = () => {
fetch(endpoint)
.then((response) => {
console.log(response.body)
})
.catch((err) => {
console.log(err);
});
};
How can I obtain the ReadableStream in a Base64 format? This is returning a png file.
I made an API call to an endpoint and it returns this:
const test = () => {
fetch(endpoint)
.then((response) => {
console.log(response.body)
})
.catch((err) => {
console.log(err);
});
};
How can I obtain the ReadableStream in a Base64 format? This is returning a png file.
Share Improve this question edited Sep 6, 2020 at 17:56 Daniel A. White 191k49 gold badges379 silver badges466 bronze badges asked Sep 6, 2020 at 17:53 Freddy.Freddy. 1,7453 gold badges23 silver badges36 bronze badges 1-
1
have you tried reading the
fetch
documentation? – Daniel A. White Commented Sep 6, 2020 at 17:57
1 Answer
Reset to default 6Using this answer, you can get the blob and convert it to base64.
const test = () => {
fetch(endpoint)
.then((response) => {
return response.blob();
})
.then((blob)=>{
var reader = new FileReader();
reader.readAsDataURL(blob);
reader.onloadend = function() {
var base64data = reader.result;
console.log(base64data);
}
})
.catch((err) => {
console.log(err);
});
};
MDN on .blob()