React Native Fetch Request Fails - react-native

I am using the react native Fetch API to get JSON data from https://api.github.com/users/{username} but the request fails with the following error message.
"TypeError: Network request failed {stack: (...), message: 'Network
request failed'}".
I believe for https, sometimes you get NSURLAuthenticationChallenge. I am not sure how to implement this. Anyone have any idea about this?

If you are using iOs you may need to do the following:
You must enable your AppTransportSecurity
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
or
in the Info.plist.
For Android ensure that you have added permission to use INTERNET in the AndroidManifest.xml.
<uses-permission android:name="android.permission.INTERNET" />

Can you provide an snippet of your fetch code?
Generally, a fetch statement is written like:
fetch(requestURL)
.then( (response) => response.json() )
.then( (data) => {
this.setState({data: data});
})
.catch( (error) => console.log(error) )
.done();
Catching the error will allow the program to proceed without crashing.

In my case the issue wasn't in the code. I started emulator when had no network. After I logged in to wifi point network on emulator still wasn't functioning. I restarted emulator - all worked.
So:
Connect to network.
Restart emulator.
If it doesn't help, then check your code.

I was also having this issue React Native Fetch Request Fails very frequently.
In my case the response from the API call was around 5kb , so i removed the unnecessary data from the API response and reduced the result size to around 1kb and all started working.
So try to limit the data you are requesting from the API.

Have you tried XMLHttpRequest?
As demonstrated in the doc, https is supported by XMLHttpRequest:
var request = new XMLHttpRequest();
request.onreadystatechange = (e) => {
if (request.readyState !== 4) {
return;
}
if (request.status === 200) {
console.log('success', request.responseText);
} else {
console.warn('error');
}
};
request.open('GET', 'https://mywebsite.com/endpoint.php');
request.send();

Related

HTTP requests does not work on my real android device (React Native Expo Tunnel)

I'm facing this issue while posting data to my server via axios.post(). It always catches the error "Network Error" when I run my app on my real android device. But when I use an android emulator device, it works correctly and returns the response. I tried to use "ngrok http 5000" and used the uri that ngrok gave me but that didn't work either. I'm hopeless at this point because my app is using QR code scanner and indie notifications. I'm unable to test it while using emulator. Please help me!
I'm leaving my axios.post() method below
const login = async (email, password, navigation) => {
userState.loading = true;
axios
.post(`http://${localIP}:5000/api/user/login`, {
email,
password,
})
.then((response) => {
userState.loading = false;
userState.user = response.data;
console.log("Data: ", response.data);
navigation.reset({
index: 0,
routes: [{ name: "Home" }],
});
})
.catch((err) => {
userState.loading = false;
console.log("ERR :", err.message);
});
};
I tried to use "ngrok http 5000" and used the uri that ngrok given me but that didn't work either.
Might be a little light on information in order to give a definite answer, it's worth looking at installing a debugger and inspecting the network requests to see where they're going wrong. But here's a couple things to try:
In your AndroidManifest.xml file add the line: <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Use http://my.local.ip:port instead of http://localhost:5000 (looks like you're doing this anyway, if not you get this via ipconfig in Command Prompt)
Try adding "Content-Type": "application/json", Accept: "application/json" headers to your Axios request

React Native with axios - Network error calling localhost with self signed https endpoint

I created service in Visual Studio with Conveyor extension to make it accessible in local network. I installed certificate on my Android device so there is green padlock when calling it in browser and service works fine.
However when calling it from React Native app with axios then i get Network error.
That's my code:
const fetchData = async () => {
console.log('url', `${Settings.API_URL}/location/GetDataByMyIp`);
try {
const result = await axios(
`${Settings.API_URL}/location/GetDataByMyIp`,
);
console.log('result', result);
ipData = {
city: result.data.city,
longitude: result.data.longitude,
latitude: result.data.latitude,
};
} catch (e) {
console.log('error', e);
}
};
await fetchData();
In console i see:
url https://192.168.1.14:45455/api/location/GetDataByMyIp
error Error: Network Error
at createError (path_to_app\node_modules\axios\lib\core\createError.js:16)
at EventTarget.handleError (path_to_app\node_modules\axios\lib\adapters\xhr.js:83)
at EventTarget.dispatchEvent (path_to_app\node_modules\event-target-shim\dist\event-target-shim.js:818)
at EventTarget.setReadyState (path_to_app\node_modules\react-native\Libraries\Network\XMLHttpRequest.js:575)
at EventTarget.__didCompleteResponse (path_to_app\node_modules\react-native\Libraries\Network\XMLHttpRequest.js:389)
at path_to_app\node_modules\react-native\Libraries\Network\XMLHttpRequest.js:502
at RCTDeviceEventEmitter.emit (path_to_app\node_modules\react-native\Libraries\vendor\emitter\EventEmitter.js:189)
at MessageQueue.__callFunction (path_to_app\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:425)
at path_to_app\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:112
at MessageQueue.__guard (path_to_app\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:373)
I tried to add this line to AndroidManifest.xml application tag but still have the error
android:usesCleartextTraffic="true"
Some solutions say about adding as 2nd parameter to axios, but it doesn't work with React Native.
new https.Agent({
rejectUnauthorized: false,
});
I tried to call some other service from the internet and then error was gone.
Is there some solution to this? I haven't found anything more. My last idea is to host service on Azure so I'll have properly signed SSL, but i guess it has to be a way to run it locally.
Edit: It works through http

Twilio chat React Native SDK Error: Can't add command

I'm using the SDK client in React Native to add chat to my app using Twilio Programmable Chat. The code to send a message is below:
client.sendMessage(message.text)
.catch(err => console.log(err));
I am getting an error back in my console which says:
Error: Can't add command: (status: 0, code: 0)
at session.js:173
at tryCallOne (core.js:37)
at core.js:123
at JSTimers.js:294
at _callTimer (JSTimers.js:151)
at _callImmediatesPass (JSTimers.js:199)
at Object.callImmediates (JSTimers.js:463)
at MessageQueue.__callImmediates (MessageQueue.js:316)
at MessageQueue.js:136
at MessageQueue.__guard (MessageQueue.js:291)
I'm catching it so it's not causing any problems in my actual app but it would be great to understand that's causing it and how to fix it.
Note: The message is sending and all functionality looks to be fine.
Thanks for any help
I was able to get rid of this issue on my end. It was due to an improper Promise chain with my leaveChannel() method. Since resolving this I have not had any issues with the add command error, which I believe was caused by the room not properly disconnecting. Below is my method for disconnect if it helps. Let me know how you make out.
leaveChannel() {
return new Promise((resolve, reject) => {
if (this.channel) {
this.channel.removeAllListeners();
this.channel
.leave()
.then((leftChannel: Channel) => {
console.log("Left chat channel: " + leftChannel.uniqueName);
store.dispatch(chatSetState(ConnectionStateEnum.DISCONNECTED));
resolve();
})
.catch((error: any) => {
console.log("leaveChannel(): ", error);
this.channel = null;
reject(error);
});
} else {
console.log("Not currently in a channel.");
}
});}

Access Dyson server from React Native ios

I'm using Dyson to host a little mock server for my React Native app, and trying to fetch from the server. The server appears to be running well and when I visit my desired url, http://localhost:3000/stations, in my browser, I get a nice JSON response.
In my React Native action, though, I get Network request failed:
export function fetchStations() {
return dispatch => {
dispatch({ type: "GET_STATIONS_START" });
fetch("http://localhost:3000/stations")
.then(res => {
return res.json();
})
.then(json => {
dispatch({ type: "GET_STATIONS_SUCCESS", payload: json.stations });
})
.catch(error => {
console.warn(error);
dispatch({ type: "GET_STATIONS_FAILURE", payload: error });
});
};
}
Using a static local URL works, and using, say, the Google Maps API works (even though it's not what I want, it's just a sample API to try).
I would think I may be calling the url wrong but it works in the browser so that seems doubtful. My guess is that is has something to do with iOS not liking http requests (only accepting https), unless you set some setting somewhere (I've been through this in native iOS development).
If this is the problem, how do I fix it from React Native? Or, what is the actual problem?
PS. I'm using dyson rather than json-server because for some reason I can't get json-server to work. See my other post. Somewhere here :)
I figured it out. The device (simulator) doesn't have access to localhost so I had to set my url to http://127.0.0.1:3000/stations and it works like a dream.

react-native iOS Simulator Network Connection

I'm trying to fetch() some data in my react-native app and it's failing. So, I found NetInfo and I am using it when the app starts and on each MapView onRegionChangeComplete event using the iOS simulator (if it matters, iPhone5s and iPhone 6), and the status returned by NetInfo is unknown every time. The MapView works fine and I can navigate the web using Safari on the simulators. The most common issue I've found related to fetch() failing is from developers using localhost in their url. I'm using http://api.openweathermap.org/data/2.5/weather? as my url, so I'm confident this is a simulator issue. I don't have a iOS device to test on and MapView isn't available for android yet, so I can't test on android. Can someone point me in the right direction as to why?
Thank you.
PS. The fetch error I'm logging to console is - TypeError: Network request failed
Here is my code:
module.exports = function(latitude, longitude) {
var rootURL = 'http://api.openweathermap.org/data/2.5/weather?APPID=MYAPIKEY&lat=35&lon=139';
console.log(rootURL);
return fetch(rootURL) // fetch is part of react or react-native, no need to import
.then(function(response){ // method chaining used here
// Shorthand to check for an HTTP 2xx response status.
// See https://fetch.spec.whatwg.org/#dom-response-ok
if (response.ok) {
console.log('Response from fetch was ok, returning response to next then');
return response
} else {
// Raise an exception to reject the promise and trigger the outer .catch() handler.
// By default, an error response status (4xx, 5xx) does NOT cause the promise to reject!
console.log('Throwing error to .catch with statusText: ' + response.statusText);
throw Error(response.statusText);
}
})
.then(function(response) {
console.log('Returning JSON to next then')
return response.json();
})
.then(function(json) { // chain a second function when JSON is returned
console.log('Returning this json to Api call in index.os.js' + json);
return {
city: json.name,
//temperature: kelvinToF(json.main.temp),
//decription: json.weather[0].description
}
})
.catch(function (error) {
console.log('My fetch Error: ' + error + 'Specific error: ' + error.message);
})
}
2nd UPDATE: In addition to the updates and information below, I've narrowed it down to this being an iOS simulator issue. I don't have the same problem on an Android device. Again - the only time the simulator has an issue is making a fetch() request using a http: url. When I use a https: url, the simulator is happy. I also found some DEV setting in the simulator to allow http requests. This gave me no change.
UPDATE: I've narrowed the problem down to the url. If I use fetch within react-native, with this url http://api.openweathermap.org/data/2.5/weather?APPID=MYAPPKEY&lat=35&lon=139 with MYAPPKEY equaling my key, it fails. If I use the same url in my browser, it returns the json I am expecting.
Also, if I use fetch within react-native and use this url https://api.github.com/users/mralexgray/repos , I don't get the Network request failed, I get valid json.
One more interesting note - if I enter the openweathermap url in my browser, the address bar strips off the http:// part when it returns json. When I enter the github address in my browser, the address bar keeps the https://.
Another interesting note - if I use a non-https url, it fails.
If you are using react-native#0.28 or higher, in the Plist.Info the following was removed:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
And this was added :
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
<key>NSAppTransportSecurity</key>
<!--See http://ste.vn/2015/06/10/configuring-app-transport-security-ios-9-osx-10-11/ -->
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>localhost</key>
<dict>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
This is related to the ATS in iOS, look at the following link for more info.
https://ste.vn/2015/06/10/configuring-app-transport-security-ios-9-osx-10-11/
You basically need to add the http domains that you use to the plist.Info