I'm working on a react program to get an image from the backend and display it in the frontend. The API call is working fine and I'm able to get the image preview in the postman while testing the API. But I couldn't convert it into an image in the front end.
Below is the response I received from the backend:
{data: '����\x00\x10JFIF\x00\x01\x01\x01\x00H\x00H\x00\x00��\x02(ICC_PROFILE\x00\x01\x01\x00\x00\x02\x18\x00\x00\x00\x00\x02\x10\x00\x00…��M�M�2(݃\x04�#�ɤ�j7�\x02�W\x137�1x�-��.b[�Y���7%���];�H��', status: 200, statusText: '', headers: {…}, config: {…}, …}
Here data is the string version of the image and I need to convert it to an image in the frontend. Below is my frontend code:
import './App.css';
import axios from 'axios';
import React, {useState, useEffect} from 'react';
function App() {
const[imageUrl, setImageUrl] = useState('')
useEffect(() => {
axios.get('http://localhost:8080/userImage/userthree@email.com/download/', {
headers: {
}
})
.then(response => {
console.log(response)
const url = URL.createObjectURL(response.data);
setImageUrl(response.data)
console.log(url)
})
.catch(err => {
console.log(err)
})
},[])
return (
<div className="App">
<img src={imageUrl} alt="Image Placeholder"/>
</div>
);
}
export default App;