I am trying to Build an Application in React native i have to perform Login User Authentication using Rest Api . Because i am new to React native i am not able to understand how to perform this action can any body help. Thanks
-
What have you tried so far?Vencovsky– Vencovsky2019-05-29 11:03:39 +00:00Commented May 29, 2019 at 11:03
-
Currently i just created a layoutS.Hashmi– S.Hashmi2019-05-29 11:12:01 +00:00Commented May 29, 2019 at 11:12
-
but don't know how to proceedS.Hashmi– S.Hashmi2019-05-29 11:12:27 +00:00Commented May 29, 2019 at 11:12
-
try using fetch or axiosVencovsky– Vencovsky2019-05-29 11:12:58 +00:00Commented May 29, 2019 at 11:12
-
please suggest a way to write code for it or suggest a sample code pleaseS.Hashmi– S.Hashmi2019-05-29 11:13:40 +00:00Commented May 29, 2019 at 11:13
Add a comment
|
1 Answer
React Native provides the Fetch API for your networking needs. Fetch will seem familiar if you have used XMLHttpRequest or other networking APIs before. You may refer to MDN's guide on Using Fetch for additional information.
Making requests
In order to fetch content from an arbitrary URL, just pass the URL to fetch:
fetch('https://mywebsite.com/mydata.json');
example.js
// Example POST method implementation:
postData('http://example.com/answer', {answer: 42})
.then(data => console.log(JSON.stringify(data))) // JSON-string from `response.json()` call
.catch(error => console.error(error));
function postData(url = '', data = {}) {
// Default options are marked with *
return fetch(url, {
method: 'POST', // *GET, POST, PUT, DELETE, etc.
mode: 'cors', // no-cors, cors, *same-origin
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
credentials: 'same-origin', // include, *same-origin, omit
headers: {
'Content-Type': 'application/json',
// 'Content-Type': 'application/x-www-form-urlencoded',
},
redirect: 'follow', // manual, *follow, error
referrer: 'no-referrer', // no-referrer, *client
body: JSON.stringify(data), // body data type must match "Content-Type" header
})
.then(res => res.json())
.then(response => console.log('Success:', JSON.stringify(response)))
.catch(error => console.error('Error:', error));
}