Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Make HTTP Requests In React Native IOS application?

fetch('http://119.9.52.47:3000/api/countries', {
       method: 'POST',
       headers: { 'Accept': 'application/json','Content-Type': 'application/json'},
   }).then((response) => response.json())
     .then((responseData) => {
           console.log(responseData);
       })

Here is my code . But that's not work.

like image 476
Saravana Kumar Avatar asked Mar 08 '17 08:03

Saravana Kumar


People also ask

How do I make HTTP request in React Native iOS?

If you need to fetch from a cleartext URL (one that begins with http) you will first need to add an App Transport Security exception. Your request is http , so you need to either add the address as an App Transport Security exception in your ios app, or use https.

Which methods are used to make HTTP request in React Native?

Making requestsAxios is a hugely popular (over 52k stars on Github) HTTP client that allows us to make GET and POST requests from the browser. Therefore, we can use Axios with React Native to make requests to an API, return data from the API, and then do things with that data in our React app.

How do I send a request in React Native?

To trigger a Post request from the UI side in react -native, we can send the Request option as a second Parameter. Project Structure: The project should look like this: Example: Here, we are sending request options as a second parameter along with the body. This body is handled in API.

Can I use REST API in React Native?

You can consume REST APIs in a React application in a variety of ways, but in this guide, we will look at two of the most popular approaches: Axios (a promise-based HTTP client) and Fetch API (a browser in-built web API).


2 Answers

you can try like this for send data to server(POST)

let response = await fetch(
    'http://your_url', {
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        username: this.state.name,//data which u want to send
        password: this.state.password,
      })
  });
  let responseText = await response.text();
  if (response.status >= 200 && response.status < 300){
    Alert.alert('Server response', responseText)

  }
  else {
    let error = responseText;
    throw error
    //Alert.alert('Login', error)
  }
} catch(errors) {
  Alert.alert('Login', errors)

  Actions.Documents();
}

Edit: As latest iOS sdk enforces connection to be in https protocol instead of http.you can add an exception to your domain inside info.plist file of the Xcode project.

if you want to allow everything write this inside info.plist

<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
    <key>yourdomain.com</key>
    <dict>
        <!--Include to allow subdomains-->
        <key>NSIncludesSubdomains</key>
        <true/>
        <!--Include to allow HTTP requests-->
        <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
        <true/>
        <!--Include to specify minimum TLS version-->
        <key>NSTemporaryExceptionMinimumTLSVersion</key>
        <string>TLSv1.1</string>
    </dict>
 </dict>
</dict>

for more info check this out https://stackoverflow.com/a/31623388/7604342

like image 166
Nitesh Mishra Avatar answered Sep 20 '22 12:09

Nitesh Mishra


You can use normal fetch function, only add you http host into exception. In your XCode.

enter image description here

like image 24
Andrey Patseiko Avatar answered Sep 21 '22 12:09

Andrey Patseiko