简体   繁体   中英

Sending authorization token with Axios GET does not work

I am making a GET request in my react native app. My code is:

const config = {
  headers: {
    Authorization: `Bearer ${authToken}`,
  },
};
axios.get(url, config);

The authorization token is not being sent along with the request. How do I send it?

You could use the same GET method in two ways.

Method 1

axios.get(your_url, {
 headers: {
   Authorization: 'Bearer ' + your_token
 }
})

Method 2

axios({
  method: 'get', 
  url: your_url,
  headers: {
    Authorization: 'Bearer ' + your_token
  }
})

For POST method

axios({
      method: 'post', 
      url: your_url,
      data: { "user_id": 1 }
      headers: {
        Authorization: 'Bearer ' + your_token
      }
    })

Please try with the any of above method and let me know.

try this:

var req = {
            url: the url ,
            method: "get",
            headers: {
                Authorization: "Bearer " + val,
                Accept: "application/json"
            }
        };

        axios(req)
            .then(response=>{console.log(response)})
            .catch(error => {});
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM