3

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
  • 788
  • 12
  • 31
mariovzc
  • 135
  • 2
  • 10

3 Answers3

9

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
  • 214
  • 2
  • 5
  • 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
1

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 .

unknown123
  • 404
  • 2
  • 10
  • 26
1

Well the problem was with the SSL certificate of the URL

mariovzc
  • 135
  • 2
  • 10