-
Notifications
You must be signed in to change notification settings - Fork 24.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot Make API call to http endpoint when using formData[Tried with both fetch and axios] #44737
Comments
|
Hello, I've come across your issue and would like to suggest a couple of potential solutions that might resolve the problem you're experiencing with Firstly, ensure that the URI for the file you're appending to Here's an updated snippet of your code with these changes: const formData = new FormData();
formData.append('audio_file', {
name: fileName,
type: 'audio/mpeg', // Correct MIME type
uri: `file://${filePath}` // Correct URI scheme
});
try {
const response = await fetch('http endpoint', {
body: formData,
method: 'POST',
headers: {
'Content-Type': 'multipart/form-data',
Authorization: 'Bearer token'
}
});
console.log('test result ', response);
console.log('test result status ', response.status);
} catch (e) {
console.error('test error ', e);
} |
Tried everything including your suggestion, still no luck 😓 |
I don't know if this helps OR is related, but we have been using FormData for 4+ years, and it broke when upgrading to 0.74.3 from 0.72, the culprit being
addition of
specifically filename* part, so I've had to patch-package and revert it for time being. I kept getting "request is malformed". YOu can debug network errors a bit better using Charles and SSL stripping, so you will see exactly what is being sent/received as well the format etc. |
@chr4ss12 Not sure why you mentioned me. This change of Check the encoded name. If the |
@robertying I saw you created the commit (sorry if that's not the case - and you only approved it). I am using very simple code to upload files to google cloud storage, and it stopped working after that commit, there's nothing I can do with the HTTP server that's handling the header (the name= is always hardcoded to "myfile.jpg"), just giving heads up to anyone else experiencing this issue. |
@chr4ss12 I did make the change. This change was necessary to support utf-8 filenames. If you could have a repro or try constructing the same header value in a form data request in Postman to isolate it, and see if it fixes the issue, it would be easier to see where it went wrong. I previously tested the multipart upload with Express.js and Multer, they received files fine. |
@robertying I've checked the code, the spec, played around the request in Charles, and everything checks out - as far as I can tell it looks all right. The problem is with google Cloud storage not knowing how to read the filename*=UTF-8''dummy.jpg part of the request - I will try and see if I can raise an issue in google tracker. In meanwhile, am not too convinced of this change though, mainly because the way they implemented the syntax in the first place: filename*=utf-8'' I. mean really? did they not want to throw in anything else that for sure will break ANY implementation that has no filename*= parsing support, unless that was their idea to begin with... Also the app I use 'Charles' which I use for debugging could not parse the multipart data because it thought it is malformed (it is not), it seems this filename*= syntax is not that well adapted, perhaps something more along side of lines
|
@chr4ss12 Yes in theory if the parser doesn't know the keys in the header value, it should throw them away, according to the doc. In practice though, I can definitely see implementations that don't conform to the spec. Feel free to file a bug and propose a fix in a PR. Thank you for investigating this. 🙏 |
We experienced this exact same issue due to this bug - it looks like it's either that we're not encoding the name correctly or the server cannot parse this value. This is an example of a header that we're getting from React Native now, which is not being parsed correctly by Go.
This is the go stdlib for mime parsing: Do we know if Should we not be using
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent |
@Tadimsky Maybe check if the server can parse the header if you get rid of the |
Ah you're right, sorry about that! It looks like MDN has an example of how to create RFC-5987 valid values for I think we should probably be using something like that in React Native? |
@Tadimsky Thanks so much for finding that. Would you like to raise a PR with that? |
Yes, let me give that a try! |
Hello, I just encountered the same problem with the Content-Disposition, and came onto saw this issue. After some digging into the MDN docs, i found that this section states that the
I hope I'm understanding that correctly. I can create a PR for this if necessary. |
My understanding is exactly that as well. This issue has caused unexpected issues for us too. |
Description
Cannot Make API call to http endpoint when using formData[Tried with both fetch and axios]
I've tried addring clearTextTrafic true in android Mainfest.xml file
Working fine in iOS, and working well with normal APIs ( without formData), only API with formData having this issue.
Steps to reproduce
const formData = new FormData()
formData.append('audio_file', {
name: fileName,
type: 'audio/mp3',
uri: filePath
})
This returns network error
React Native Version
0.74.1
Affected Platforms
Runtime - Android
Output of
npx react-native info
Stacktrace or Logs
Reproducer
private repo cannot provide link
Screenshots and Videos
No response
The text was updated successfully, but these errors were encountered: