coderzzz18
coderzzz18

Reputation: 2615

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();

}

Upvotes: 8

Views: 18843

Answers (1)

manpenaloza
manpenaloza

Reputation: 385

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.

Upvotes: 11

Related Questions