Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get response header in react native android?

Hello i want to get response header after fetch POST request. I tried to debug to see what inside response with console.log(response). I can get response bodies from responseData but i have no idea how to get the header. I want to get both header and the body. Please help. thanks:)

Here's the example what i've done:

   fetch(URL_REGISTER, {
      method: 'POST',
      body: formData
    })
      .then((response) => response.json())
      .then((responseData) => {
        if(responseData.success == 1){
          this.setState({
            message1: responseData.msg,
          });
        }
        else{
          this.setState({
            message1: responseData.msg,
          });
        }
      })
      .done();
    }, 

1 Answers

you can do like this

  fetchData() {
  var URL_REGISTER = 'https://www.example.com';
  fetch(URL_REGISTER, {method: 'POST',body: formData})
      .then(
          function(response) {
              console.log(response.headers.get('Content-Type'));
              console.log(response.headers.get('Date'));

              console.log(response.status);
              console.log(response.statusText);
              console.log(response.type);
              console.log(response.url);
              if (response.status !== 200) {
                  console.log('Status Code: ' + response.status);
                  return;
              }

              // Examine the text in the response
              response.json().then(function(data) {
                  console.log(data);
              });
          }
      )
      .catch(function(err) {
          console.log('Fetch Error', err);
      });
}

Read More about fetch: Introduction to fetch()

like image 125
Ashok R Avatar answered Mar 05 '26 22:03

Ashok R



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!