mariovzc
mariovzc

Reputation: 213

React Native Http request dont work in Android

Working with requests to the server using Fetch and also using Axios, when running it on Android emulator/devise it shows me the following error:

enter image description here

this is the request code:

fetch(URL,{
      method: 'POST',
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
                email: userEmail,
                password: userPassword
            }),

        })
        .then((response) => response.json())
         .then((responseJson)=>{
             Alert.alert("bien");
       console.warn(responseJson);
             })
         .catch((error)=>{
         console.error(error);
     console.warn(error);
   });

Upvotes: 6

Views: 24315

Answers (4)

AbdulRehman
AbdulRehman

Reputation: 1026

For those trying out in the iOS, here is the updated version of the answer given by @iamsr

the info.plist looks like this:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>localhost</key>
        <dict>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
        </dict>
    </dict>
</dict>

and you can add another key below inside the current NSAppTransportSecurity

so the final result to be:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>localhost</key>
        <dict>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
        </dict>
    </dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

Upvotes: 2

iamsr
iamsr

Reputation: 412

Since Http request doesn't work in the latest android updates I think after 28 arrived. So you have to add the following attributes to your AndroidManifest.xml

    <manifest 
        xmlns:tools="http://schemas.android.com/tools">

        <uses-permission android:name="android.permission.INTERNET" />

        <application
           android:usesCleartextTraffic="true"> 

                // ----------------

        </application>
   </manifest>

Upvotes: 20

mariovzc
mariovzc

Reputation: 213

Well the problem was with the SSL certificate of the URL

Upvotes: 4

ajithes1
ajithes1

Reputation: 427

Go to info.plist (ProjectFolder->ios->ProjectFolder->info.plist)and add the following before </plist>

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

Now restart project once again .

Upvotes: 1

Related Questions