axios: Axios don't send form data in react-native
I’ve try to send a form data with axios but i can’t, in chrome dev tools no content appears in the request. Making a same request via postman the API response normally.
(newData) => {
const data = new FormData();
data.append('name', 'raphael');
data.append('file', {
uri: newData.image.path,
type: 'image/jpeg',
name: 'teste'
});
return axios.post(`${constants.development.URL_API}/file`, data,
headers: {
'Content-Type': 'multipart/form-data',
},
})`
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 7
- Comments: 46 (4 by maintainers)
Y’all, I appreciate the direction but “just test a file upload in React Native” isn’t that easy. I don’t often work in React Native and it would take a decent amount of time to spin up a project with file uploads to test with.
I do have a simple POST with form data that works in React Native:
true
gets stringified when sent to the server, which is interesting.That said, if anyone can provide an example that I can easily spin up, I’m happy to look into it more.
This bug still happens with latest axios, why was this closed?
@emilyemorehouse File uploads work perfectly in a browser environment, they only fail on react native. As you mentioned yourself - your tests were outside of react native environment, I would start there 😃
Try a simple file upload in react native and simply check the wireshark traces, you don’t even need a working server…
i have same issues and my body when i check in debug is string
and here my code
Update : An error occurred because the app was in debug mode. When i turn off debug mode , it worked
For anyone that this may be of help, if you don’t know the type of the content that will be sent over, use the
multipart/form-data
. It sounds obvious reading this now, but it took me a good hour to get any media working.@tkserver if your debugger has network inspect enabled, it will override the react-native’s FormData and will result in passing empty FormData object which will look like [Object object] in network inspect. Try making request with turning off network inspect, hope this helps.
@corujoraphael I’m not sure that your code runs as-is. The config where you specify your headers should be an object, but it’s missing a curly brace:
Since this issue hasn’t received a response and this doesn’t look like an Axios bug based on the info we have, I’m closing this issue.
If you need further assistance debugging your code, please post on Stack Overflow, Gitter, or comment again on this issue.
Thanks!
I have the same issue on react native when I post to a url, as @giladno mentions, you should test in react native and you could verify the error that people report @emilyemorehouse. I report an error 14 days ago https://github.com/axios/axios/issues/1618, but nobody has responded, please describe in the readme of your library that you do not have support for axios on react native, so that many developers do not waste time…!
@duongtranpyco I’ve removed axios from my project, I wrote my own simple class instead, enjoy!
P.S. If you mainly sending JSON, modify the code. In my case, the server usually expects a form.
If people are still experiencing this issue, and you have
GLOBAL.XMLHttpRequest = GLOBAL.originalXMLHttpRequest || GLOBAL.XMLHttpRequest;
snippet to debug network requests, try addingGLOBAL.FormData = GLOBAL.originalFormData || GLOBAL.FormData
it worked for me.Second @giladno. Since I’m going forward on other parts, I use a workaround instead: write a specific function with fetch() when uploading images.
Tried before and neither stringifying the data or arguments affect the result. The server received the call but no data sent.
Can you tell me how did you build the request so it uploads properly as multiparty? I keep getting a network error:
UPDATE: manage to make it work now. Currently using:
I had this problem, but it’s fixed now.
The reason for it was that I was debugging network requests. I had this line of code in my app’s entry point:
I commented it and it works now.
I did have the some problem in react-native. The problem are not the Headers. In web we can have for example:
but in react native the value for key must be string.
formData.append('key', 'true')
Hi, I am facing the same problem. Has anybody found a solution?
For this case use fetch 😄
@emilyemorehouse , I met the same problem using React Native FormData
Expect some key:value pairs exist in MultipartForm, Form, or PostForm
2018/02/20 18:25:31.740 [W] [photo.go:411] req.MultipartForm: nil 2018/02/20 18:25:31.740 [W] [photo.go:412] req.Form: map[] 2018/02/20 18:25:31.740 [W] [photo.go:413] req. PostForm: map[] 2018/02/20 18:25:31.740 [W] [photo.go:410] req: &{POST /uploadphoto/dialog/57120e8951c643ab42a8c19f/000000000000000000000001 HTTP/1.1 1 1 map[Content-Type:[application/json;charset=utf-8] User-Agent:[okhttp/3.6.0] Accept:[application/json, text/plain, /] Content-Length:[419] Connection:[Keep-Alive] Accept-Encoding:[gzip] Cookie:[lang=zh-TW; PVsessionID=db9a21d63b2d0ea47b68fa8755bd87e2]] 0xc420e3cb80 <nil> 419 [] false 10.0.1.2:8888 map[] map[] <nil> map[] 10.0.1.3:46904 /uploadphoto/dialog/57120e8951c643ab42a8c19f/000000000000000000000001 <nil> <nil> <nil> 0xc420e3cb40} 2018/02/20 18:25:31.740 [E] [photo.go:425] [UploadPhotos] err: request Content-Type isn’t multipart/form-data
Version info axios: 0.16.2 expo: 25.0.0 react: 16.2.0 react-native: 0.52.0