Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native Post Request via Fetch throws Network Request Failed

I've came across the following error. At the moment I developing an Android App with React Native therefore I'm planning to use fetch for doing a post request for me.

fetch("https://XXreachable-domainXX.de/api/test", {
    method: "post",

    body: JSON.stringify({
      param: 'param',
      param1: 'param',

    })
  })
  .then((response) = > response.json())
  .then((responseData) = > {
    ToastAndroid.show(
      "Response Body -> " + JSON.stringify(responseData.message), ToastAndroid.SHORT
    )
  })
  .catch((error) = > {
    console.warn(error);
  });

The app now throws an error:

TypeError: Network request failed

When I change the code to a GET-Request it's working fine, in the browser with a window.alert() as a return it's cool and also the Chrome extension Postman returns data correctly.

like image 586
arnebr Avatar asked Jan 02 '16 20:01

arnebr


People also ask

How do you solve network request failed in React Native?

It's just that simple! Start your app as usual but don't forget to give an IP address and a port, this will help you solve the Network Request Failed error. And on your mobile app, make sure to use the correct URL in your request. Make sure that CORS is enabled on your backend to avoid errors related to CORS.

How do I send a post request in React Native?

To trigger a Post request from the UI side in react -native, we can send the Request option as a second Parameter. Project Structure: The project should look like this: Example: Here, we are sending request options as a second parameter along with the body. This body is handled in API.

What does it mean when it says network request failed?

Most of time , when network request failed error occurred, clearing the app cache will resolve the issue. So, To fix Instagram network request failed issue, clear Instagram app cache. To clear the Instagram App cache, For Android users, Go to settings >> Find Instagram App >> Tap on clear cache.

How fetch data from API in React Native?

1. fetch method: The fetch() method in JavaScript is used to request to the server and load the information in the webpages. The request can be of any APIs that return the data of the format JSON or XML. This method returns a promise.


3 Answers

This React Native's error is rather useless, so you need to get the actual underlying error first. The most straightforward way is to write a small native program that would just perform the same query using HttpsURLConnection.

For me the actual error was java.security.cert.CertPathValidatorException: Trust anchor for certification path not found. which has a well known solution: https://developer.android.com/training/articles/security-ssl.html#MissingCa

This is quite likely your case also, given that the browsers and Postman have no problem with the request. To check it run openssl s_client -connect XXreachable-domainXX.de:443 -showcerts. If there are certificate errors, fix them first, it could spare you time writing the native program.

Edit: actually the easiest way to see all underlying android errors for react native is simply running 'adb logcat' in terminal

like image 103
Antonio Avatar answered Sep 22 '22 21:09

Antonio


Developing with Windows OS/PHP built-in server/react-native Android on device:

  • check server local IP address (ipconfig), e.g. 172.16.0.10
  • in react-native fetch use this URL and proper port (fetch('http://172.16.0.10:8000/api/foo))
  • run PHP built-in server with this specific IP instead of the localhost: php -S 172.16.0.10:8000 ...
  • turn off Windows firewall for the private networks

That fixed the connection problem between Android phone and the local server for me.

like image 41
mp31415 Avatar answered Sep 26 '22 21:09

mp31415


None of the other answers helped me. The problem was headers:

Old header:

fetch(API_HOST, {
                method: 'POST',
                headers: {
                    Accept: 'application/json'
                },
                body: JSON.stringify(data),

Updated header:

fetch(config.API_HOST, {
                method: 'POST',
                headers: {
                    Accept: 'application/json',
                    'Content-Type': 'application/json'  // I added this line
                },
                body: JSON.stringify(data),
like image 23
Arash Younesi Avatar answered Sep 22 '22 21:09

Arash Younesi