Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i get the status of my response when using fetch in react-native?

I am making Log In page for my react native application. My api sends different response when my username and password are valid and invalid. So I want to track and save the status of my response in some state variable and then later perform function accordingly. Please suggest me way to do that.

  doSignUp() {
   console.log("inside post api");
   fetch('MyApiUrl', {
      method: 'POST',
      headers: {
                 'Accept': 'application/json',
                 'Content-Type': 'application/json',

                },


      body: JSON.stringify({
      password: this.state.password,
      email:this.state.email,
      name: this.state.firstname,
      last_name :this.state.last_name,
      mobile:this.state.mobile,
      ssn:"2222222"

     })
      }).then((response) => {console.log('response:',response.status);
             response.json()}
        .then((responseData) => {
                                console.log("inside responsejson");
                                console.log('response object:',responseData)
                                console.log('refresh token:',responseData[0].token.refresh_token)
                                console.log('access token:',responseData[0].token.access_token);



         }).done();

}

like image 1000
coderzzz18 Avatar asked Sep 29 '16 09:09

coderzzz18


1 Answers

As far as I understand you want to know the http status code of your fetch request.

Usually your response object includes a "status" property. So you should be able to receive the status code by using this:

response.status

In case of a successful request this will return 200.

like image 115
manpenaloza Avatar answered Dec 28 '22 11:12

manpenaloza