title: How to pass authorization token in header for GET method using XMLHttpRequest in react native
body: <p>I am new to react-native. I am trying to pass the authorization token through a header in the GET method. But I am getting an unauthorized error.</p>
  
  <p>I have already tried this code "<a href="https://stackoverflow.com/questions/30203044/using-an-authorization-header-with-fetch-in-react-native">Using an authorization header with Fetch in React Native</a>" not working for me and also with <code>XMLHttpRequest()</code></p>
  
  <p>But the API works fine in postman, Java(core) and Android. </p>
  
  <p>Do we have any special implementation in react-native to pass headers? 
  Could anyone can help me with this?</p>
  
  <p>My code: Changed the server name.</p>
  
  <pre><code>getData() {
      var data = null;
  
      var xhr = new XMLHttpRequest();
      xhr.withCredentials = true;
  
  
      xhr.addEventListener("readystatechange", function () {
        if (this.readyState === 4) {
          console.log(this.responseText);
        }
      });
  
      xhr.open("GET", "https://xyz-test-server.server.com/api/v3/users/details/");
      xhr.setRequestHeader("Authorization", "Basic cC5qYWltdXJ1Z2FuLm1jYUBnbWFpbC5jb206MTIzNDU2");
      xhr.setRequestHeader("User-Agent", "PostmanRuntime/7.17.1");
      xhr.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded; charset=ISO-8859-1");
      xhr.setRequestHeader("Accept", "*/*");
      xhr.setRequestHeader("Cache-Control", "no-cache");
      xhr.setRequestHeader("Postman-Token", "d8ae56bf-1926-44e4-9e94-23223234,93a110a2-ee8e-42d5-9f7b-45645ddsfg45");
      xhr.setRequestHeader("Accept-Encoding", "gzip, deflate");
      xhr.setRequestHeader("Connection", "keep-alive");
      xhr.setRequestHeader("cache-control", "no-cache");
  
      xhr.send(data);
  }
  </code></pre>
  
  <p><strong>Fetch method:</strong></p>
  
  <pre><code>async _getProtectedQuote() {
    fetch('https://xyz-test-server.server.com/api/v3/users/details/', { 
      method: 'GET', 
      headers: new Headers({
        'Authorization': 'Basic cC5qYWltdXJ1Z2FuLm1jYUBnbWFpbC5jb206MTIzNDU2', 
        'Content-Type': 'application/x-www-form-urlencoded'
      }), 
    }).then(responseJson =&gt; {
      alert(JSON.stringify(responseJson));
      console.log(responseJson);
  
    });
  }
  </code></pre>