-
Notifications
You must be signed in to change notification settings - Fork 24.5k
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
Fetch call without a Content-Type throws a "Network request failed" error on Android #30176
Comments
I have the same problem here, only on Android by the way. I created a whole new project with import React, { useEffect, useState } from 'react';
import { ActivityIndicator, Text, View } from 'react-native';
export default App = () => {
const [isLoading, setLoading] = useState(true);
const [data, setData] = useState([]);
useEffect(() => {
fetch('https://reqres.in/api/users', {
method: 'POST',
headers: {
// Enabling the next line will solve the error, but it shouldn't make a difference
// 'Content-Type': 'application/json',
},
body: JSON.stringify({
"name": "morpheus",
"job": "leader"
})
})
.then(response => response.text())
.then(dat => setData(dat))
.catch(error => setData(error.message))
.finally(() => setLoading(false));
}, []);
return (
<View style={{ flex: 1, padding: 24 }}>
{isLoading ? <ActivityIndicator /> : <Text>{data}</Text>}
</View>
);
}; It's failing very consistently. react-native: 0.63.3 |
@peternoordijk, so have you tried setting the content-type? As it's commented out in your code. |
@laurent22 exactly, it will work with the content type enabled, but it shouldn't make a difference like you said |
Not working app crash directly, Also I have add to Content-Type and Accept but still not working... please help... any solutions |
@devang-p-eq do you have an error message and a stack trace? Or some code to reproduce the crash with? |
[Sat Oct 24 2020 15:18:30.828] LOG error [TypeError: Network request failed] getting msg in terminal and added network security files also in manifest android file version in my app |
I am making http requests with the fetch function and it works correctly in debug environment but when I compile the apk in release mode the fetch function does not work in versions higher than android 10, I used an android 7 physical device and it works without problems. the react native version is 0.63 |
solve my problem by adding these lines in my manifest.xml file |
Thanks for the reply Thanks to all |
The fetch library used in react-native has been upgraded in 0.63 which is probably causing this issue JakeChampion/fetch#823 My workaround was to explicitly set content-type when I run fetch() |
Is this ever going to be fixed? This bug has been there for a long time now (in several GitHub issues) and still there doesn't seem to be a good fix/solution for it. Have already tried most of the workarounds/suggestions etc but still not working on Android, iOS works fine. |
I am facing the same problem even with adding Content-Type in the call as explained here: https://stackoverflow.com/questions/67751230/why-is-fetch-query-always-returning-error-segment/ Any suggestions. |
What is this wizardry? It just fixed my problem. |
i am facing this issue while i login in my expo app from android emulator but if i login from my mobile it allow me to login |
Hi, did anyone find some solution for it? I cannot sleep because of that. When I use postman for api requests everything works fine, when I switch to android emulator it shows me this message. |
@GrzegorzSzwed |
Hi @devang-p-eq! Thank you for the reply but I am using SSL :/ The problem is quite interesting because in the browser it works too. |
Could you show more details about the http lib? |
This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days. |
This issue was closed because it has been stalled for 7 days with no activity. |
this issue is still relevant. |
Description
On Android, the following code used to work in React Native 0.61:
But with React Native 0.62 and 0.63 (tested both) it gives the dreaded:
The above code can be fixed by setting the content type in the header, so by adding this line it works again:
So I think there are three issues:
All other frameworks and libraries I've tried support the above call without having to set the Content-Type, and React Native also used to work, so something was changed that makes it mess with the Content-Type. Maybe it "intelligently" detects the type and actually sends garbage in one way or another.
If React Native requires a Content-Type for some reason, it should send back a friendly error message such as "Please set the Content-Type" instead of the generic impossible-to-debug "Network request failed".
Any such non-standard fetch behaviour should be documented.
React Native version:
Steps To Reproduce
Run the above code, or any PROPFIND fetch call without a Content-Type header.
Expected Results
That the http call succeed like it did for several years.
Snack, code example, screenshot, or link to a repository:
See above code
The text was updated successfully, but these errors were encountered: