I am making a js POST api call to a server. It responds back with a .jpg image. Here is my code:
function arCall(url = '', data = {}) {
var ARCSRFToken;
fetch('/session/token')
.then(res => res.text())
.then(text => {
ARCSRFToken = text
})
.then(() => {
//console.log(ARCSRFToken)
fetch(url, {
method: 'POST',
mode: 'cors',
headers: {
'Accept': 'text/html, */*',
'X-CSRF-Token': `${ARCSRFToken}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(data)
})
.then((res) => {
console.log(res)
return res.text();
})
.then(data => console.log(data))
})
}
arCall('<The URL>', { id: 100 });
For the console.log(data)
line, I get a response [img name].jpg
. But, the image itself doesn't display on the page.
I looked at this question Fetch image from API and altered my code based on it but that still did not seem to do the trick. Now, all I get is a URL and when I open it, it says page not found.
function arCall(url = '', data = {}) {
var ARCSRFToken;
fetch('/session/token')
.then(res => res.text())
.then(text => {
ARCSRFToken = text
})
.then(() => {
//console.log(ARCSRFToken)
fetch(url, {
method: 'POST',
mode: 'cors',
headers: {
'Accept': 'text/html, */*',
'X-CSRF-Token': `${ARCSRFToken}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(data)
})
.then((res) => {
console.log(res)
return res.blob();
})
.then(imageBlob => {
const imageObjectURL = URL.createObjectURL(imageBlob)
console.log(imageObjectURL)
})
})
}
arCall('<The URL>', { id: 100 });
The only thing I want to do is display the .jpg picture on the page.