I have use expo-image-picker
and axios 0.26.1
. formData
in axios
version 0.26.1 isn't work. when using formData
, data is not sent to the api
downgrade axios
to version 0.24.0 but I get this error when sending formData in Android emulator.
Error: Network Error
formData:
export const sendRequest = (url, response, method, formData) => {
return axios({
url,
method,
data: method !== "get" ? formData : null,
headers: {
Authorization: `Bearer ${response.data.access}`,
transformRequest: (data, headers) => {
return formData;
},
},
});
const formData = new FormData();
const imageUri = image.value.uri;
const newImageUri = "file:///" + imageUri.split("file:/").join("");
formData.append("photo", {
uri: newImageUri,
type: mime.getType(newImageUri),
name: newImageUri.split("/").pop(),
});
data.append("title", title);
can you help me please?
This bug has been posted on Axios GitHub issues and apparently it is present since 0.25.0. I had to roll back to version 0.24.0 to make it work. However, this issue has been closed with the following solutions.
Method 1:
const formData = new FormData();
const response = await axios.post('/URL', formData, {
headers: {
...formData.getHeaders()
}
});
Method 2:
axios.post(url, formData, {
headers: { 'Content-Type': 'multipart/form-data' },
transformRequest: formData => formData,
})
You can check the issue here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With