1

I am trying to do an axios post in react native. In postman, it is working but in app, it is not working. Can someone help?

var Data = {
        name: name,
        email: email,
      };
    }
   axios
      .post('http://10.0.2.2:8000/api/register', Data)

      .then(res => {
        alert(res);
      })
      .catch(err => console.log(JSON.stringify(err)));
  }

enter image description here

this is the error

{"message":"Network Error","name":"AxiosError","stack":"AxiosError: Network Error\n at XMLHttpRequest.handleError (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.sukprsavam&modulesOnly=false&runModule=true:103294:16)","config":{"transitional":{"silentJSONParsing":true,"forcedJSONParsing":true,"clarifyTimeoutError":false},"adapter":["xhr","http"],"transformRequest":[null],"transformResponse":[null],"timeout":0,"xsrfCookieName":"XSRF-TOKEN","xsrfHeaderName":"X-XSRF-TOKEN","maxContentLength":-1,"maxBodyLength":-1,"env":{},"headers":{"Accept":"application/json, text/plain, /","Content-Type":"application/json"},"method":"post","url":"http://103.161.55.43/api/register/","data":"{"name":"dadkn","email":"[email protected]","baby_date":"27/12/2022"}"},"code":"ERR_NETWORK","status":null}

9
  • you need to pass your ip address in url then check Commented Dec 27, 2022 at 7:32
  • @MahammadMomin can u share how my uri will look like so i can try Commented Dec 27, 2022 at 7:35
  • I think he meant to say that ip addresses you are using in postman and the app are not the same. Commented Dec 27, 2022 at 7:38
  • react native requires this ip beacuse it is a emulator @NirajNiroula Commented Dec 27, 2022 at 7:44
  • 1
    @NirajNiroula stackoverflow.com/questions/74750780/… have a look at this Commented Dec 27, 2022 at 7:51

1 Answer 1

2

IOS and Android don't work with HTTP protocol by default. It's not secure. If you can migrate to HTTPS, all will work fine.

But you can disable this secure option by updating the following files;

Android (AndroidManifest.xml)

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

    <application
       //-------add this attribute-------//
       android:usesCleartextTraffic="true"> 
    
    </application>

IOS (Info.plist)

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.