Home > database >  How to make a POST request in react native
How to make a POST request in react native

Time:12-28

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}

CodePudding user response:

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>
  • Related