I am not able to receive image sent from axios/react native at express js server. It works fine when i sent the same request to server from Postman/ThunderClient.
Here is a code which i have used in react native.
const pickImage = async () => {
let result = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ImagePicker.MediaTypeOptions.Images,
allowsEditing: true,
aspect: [4, 3],
quality: 1,
});
if (!result.canceled) {
setImage(result.assets[0]);
}
};
const createFormData = async (body: any = {}) => {
const data = new FormData();
if (image) {
const response = await fetch(image.uri || "");
const blob = await response.blob();
data.append("image", blob, "profile.jpg");
}
Object.keys(body).forEach((key) => {
data.append(key, body[key]);
});
console.log(blob);
return data;
};
const uploadImage = async (id: string, access_token: string) => {
const body = await createFormData({ id });
const response = await axios.post(
`${API_BASE_URL}user/validate/upload-profile-image`,
body,
{
headers: {
Authorization: `Bearer ${access_token}`,
},
transformRequest: [(data) => body],
validateStatus: function (status) {
return status < 500;
},
}
);
return response;
};
Here is what i am getting on console logging blob – {“_parts”: [[“image”, [Blob]], [“id”, “af1ee491-61c9-4368-93ef-813256b041dc”]]}
I have tried everything on it, doesn’t seem to be working anything. Any suggestions appreciated.
Thanks.
Pushpak Kamboj is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.