-
-
Notifications
You must be signed in to change notification settings - Fork 11.4k
Closed
Description
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',
},
})`verybluebot, ditorahard, msqar, LuongTruong, jackvial and 2 more
Activity
pedpess commentedon Jan 30, 2018
@corujoraphael Out of curiosity, was this request working earlier?
emilyemorehouse commentedon Feb 11, 2018
@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!
Ernie6711 commentedon Feb 20, 2018
@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 419 [] false 10.0.1.2:8888 map[] map[] map[] 10.0.1.3:46904 /uploadphoto/dialog/57120e8951c643ab42a8c19f/000000000000000000000001 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
giladno commentedon Mar 9, 2018
This bug still happens with latest axios, why was this closed?
emilyemorehouse commentedon Mar 9, 2018
If someone can provide a reproducible example, I'm happy to open this back up.
@Ernie6711 it looks like you have an error in your code -- I don't think you should stringify the file data before appending it to your form data. Also, the arguments for axios.post are
axios.post(url[, data[, config]])Here's an example based on your code:
giladno commentedon Mar 9, 2018
@emilyemorehouse please check wireshark traces. The data is uploaded but it does NOT use proper form-data encoding. The above example can reproduce the problem. Again - the request is being sent successfully but is it badly encoded.
emilyemorehouse commentedon Mar 9, 2018
I have a local client and server example for file uploads, albeit outside of the React Native environment, that works and is properly encoded. The example above isn't really reproducible since it relies on variables and a server that I don't have access to.
My guess is that if there's something incorrect with the upload, it's because the data appended to the form data object is not valid.
giladno commentedon Mar 9, 2018
@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...
duongtranpyco commentedon Mar 16, 2018
I got the same, Axios does not on react native
giladno commentedon Mar 17, 2018
@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.
jlariza commentedon Apr 19, 2018
Hi, I am facing the same problem. Has anybody found a solution?
Ernie6711 commentedon Apr 20, 2018
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.
jlariza commentedon Apr 24, 2018
@Ernie6711 apparently the headers are corrupted and that is why the server is unable to accept the form data. I implemented the workaround with fetch() and it worked with no problems.
20 remaining items
cesar3030 commentedon Aug 5, 2019
Thanks for your help! I was able to upload my picture with the iOS Simunator but not with the android one.
After I did set the image type to 'image/jpeg' I stopped having the Network Error.
mkhoussid commentedon Aug 26, 2019
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.AravindhNextbrain commentedon Dec 3, 2019
Hi, I am trying to file Axios upload in Vue.Js. Can anyone review and explain the issue?
In Html
<input type="file" id="file" ref="file" multiple @change="assignFiles"/>In Js
` assignFiles(e){
So, while trigger the post, am appending form datas.
`proceedSave(e) {
}`
So In the post, I checked console window header, there its showing
In My server side i cnt to get the files.
mkhoussid commentedon Dec 3, 2019
Might have something to do with
Try
files['+' + i + '+']azizulhaq800804 commentedon Dec 3, 2019
I have the same issue with fetch api. req.body was empty in node js. Using 'express-formidable' solved the issue. https://www.npmjs.com/package/express-formidable
AravindhNextbrain commentedon Dec 3, 2019
Sry + indicates the concatination, wat is the use of string '+' . It result will be files[+0+]
mkhoussid commentedon Dec 3, 2019
Did you include
app.use(express.json({ extended: false }));?mkhoussid commentedon Dec 3, 2019
Your current code will always result in
files[+i+]in your for loop. It will never befiles[+0+], files[+1+], files[+2+], etc.Also,
files[+0+]will never exist. Not sure what you are trying to do. You needfiles[0], files[1], files[2]...? If you're trying to build an object, then you can havefiles['+0+'], but, again, I'm not sure what you're trying to accomplish.azizulhaq800804 commentedon Dec 5, 2019
azizulhaq800804 commentedon Dec 5, 2019
I am using this code in my project. I am trying to understand this code. One question, is this line necessary,
for (let value of [].concat(data[key]))? It can be written simply,
pairs.push([
${key},${data[key]}]);SumitThinktanker commentedon Feb 11, 2020
Where it is?
genglei01 commentedon Feb 20, 2020
I also had the problem. It is caused by the file path in android.
const path = utils.isAndroid() ?
file://${filePath}: filePath;And it works ok with implementing as the following
Geteasymoveinc commentedon Apr 13, 2020
Use name like :
name:'teste.jpeg'and it will work. you need to give format to file otherwise it will not work.I fixed it by having name:'example.jpeg'
fatihayyildiz commentedon May 2, 2020
After several days trying to upload video with axios, I just give up. RNFetchBlob is better option for uploading files from especially Android. This section is easy to implement
rn-fetch-blob