How to get the status of my response when using fetch in native-native?

I am making a login page for my own native application. My api sends another response when my username and password are valid and invalid. Therefore, I want to track and save the status of my response in some state variable, and then execute the function accordingly. Please suggest me a way to do this.

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

}

+4
source share
1 answer

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

Typically, your response object includes the "status" property. So you should be able to get the status code using this:

response.status

200.

+5

Source: https://habr.com/ru/post/1656197/


All Articles