I get the following error when I set the ‘Content-Type’ as ‘multipart/form-data’ in react-native.
Below is my code –
const formData = new FormData();
formData.append('org_id', org_id);
formData.append('ans', userAns);
formData.append('remark', userRemark);
formData.append('img', userImg);
files.forEach(file => {
formData.append('files', {
name: file.fileName,
type: file.type,
uri: file.uri,
});
});
const resp = await multiPartInstance({
method: 'PUT',
url: `${apiBaseUrl}/installation/${Iid}/answer/${qid}`,
data: formData,
});
return Promise.resolve(true);
I am using axios for calling apis. multiPartInstance is an axios instance –
const multiPartAccessToken = async (config: AxiosRequestConfig) => {
config.headers = {
Accept: 'application/json',
access_token: useTokenStore.getState().accessToken,
'Content-Type': 'multipart/form-data;',
};
config.timeout = 30000;
return config;
};
I’ve tried the above with fetch also but I keep getting the same error. The strangest part is that this request hits the server, server sends a response too but I get this error react-native side. I’ve noticed if I don’t use FormData I don’t get any error. But I need to use FormData as I have to upload image files.
Environment Details –
- Windows version 21H2 (OS Build 22000.376)
- react-native 0.66.3
- react 17.0.2
- axios ^0.24.0
- react-native-image-picker ^4.3.0 (used for selecting images)
- Flipper version 0.99.0
I’ve tried the solutions posted on below forums but they didn’t work for me.
2
Answers
Somehow react-native-blob-util doesn't give this error. I modified my code as below -
I am as follow and works perfectly: