4

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);
   });
doganak
  • 798
  • 14
  • 31
mariovzc
  • 173
  • 1
  • 2
  • 12

4 Answers4

14

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>
iamsr
  • 314
  • 2
  • 6
  • thanks this worked and HTTP requests are working now, but HTTPS requests are not working anymore, do you know why this is happening? – DevineDecrypter Apr 14 '22 at 12:48
2

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 .

ajithes1
  • 433
  • 2
  • 11
  • 28
2

Well the problem was with the SSL certificate of the URL

mariovzc
  • 173
  • 1
  • 2
  • 12
1

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>
AbdulRehman
  • 946
  • 9
  • 16