From the following link, I get an image. Unfortunately, it shows the image in the preview tab of inspect element of the network. but in res.data it shows broken text. How will I be able to use this image in img tag like . I also shared what it returns to me in console and preview
JavaScript
x
7
1
export const getProfilePhoto = async (userType) => {
2
return await axios.get(`${API_URL}/staff/profile-photo`,{ headers : authHeader(userType) })
3
.then((res) => {
4
console.log(res.data)
5
})
6
}
7
This is what it shows in console
this is what it shows in preview
highly expecting some suggestions and solution
I’m sorry for any mistakes.
Advertisement
Answer
Ideally, you find a way to access this image without needing the extra headers, so you can just let the browser handle it directly:
JavaScript
1
2
1
<img src="{API_URL}/staff/profile-photo" />
2
If you can’t do that, then you will need to get a blob and create an object URL.
JavaScript
1
9
1
fetch(
2
`${API_URL}/staff/profile-photo`,
3
{
4
headers: authHeader(userType)
5
}
6
).then(res => res.blob()).then((blob) => {
7
img.src = URL.createObjectURL(blob);
8
});
9