javascript - How do you make an API request using browser fetch with token auth - Stack Overflow

I'm trying to make an API request using fetch(browser). A token is required in the headers to make

I'm trying to make an API request using fetch(browser). A token is required in the headers to make the request.

I can make successful requests in node (server side). However, when making requests on the browser, the OPTIONS request fails with 401.

const order_url = new URL(process.env.API_URL + 'orders/');
const params = { type: 'amazon', status: 'in_queue' };
Object.keys(params).forEach(key => url.searchParams.append(key, params[key]));

const headers = {
    Authorization: 'Token ' + process.env.API_TOKEN,
    'Content-Type': 'application/x-www-form-urlencoded'
};

fetch(order_url, {
  headers
})
  .then(response => response.json())
  .then(result => {
    console.log(result);
  })
  .catch(error => {
    console.error(error)
  })

The error i receive is "NetworkError when attempting to fetch resource." What would be the correct configuration for this to work on the browser?

I'm trying to make an API request using fetch(browser). A token is required in the headers to make the request.

I can make successful requests in node (server side). However, when making requests on the browser, the OPTIONS request fails with 401.

const order_url = new URL(process.env.API_URL + 'orders/');
const params = { type: 'amazon', status: 'in_queue' };
Object.keys(params).forEach(key => url.searchParams.append(key, params[key]));

const headers = {
    Authorization: 'Token ' + process.env.API_TOKEN,
    'Content-Type': 'application/x-www-form-urlencoded'
};

fetch(order_url, {
  headers
})
  .then(response => response.json())
  .then(result => {
    console.log(result);
  })
  .catch(error => {
    console.error(error)
  })

The error i receive is "NetworkError when attempting to fetch resource." What would be the correct configuration for this to work on the browser?

Share Improve this question edited Sep 5, 2023 at 11:50 VLAZ 29.2k9 gold badges63 silver badges84 bronze badges asked Jan 20, 2019 at 21:51 Duncan GichimuDuncan Gichimu 4661 gold badge5 silver badges17 bronze badges 8
  • Is this node environment? – kockburn Commented Jan 20, 2019 at 21:55
  • 1 Is the api CORS enabled? – charlietfl Commented Jan 20, 2019 at 22:08
  • This is the browser environment and CORS is enabled in the API. – Duncan Gichimu Commented Jan 20, 2019 at 22:11
  • 2 @DuncanGichimu is this a bearer token? If so, you need the bearer header like so: 'Authorization': 'Bearer ' + process.env.API_TOKEN, – Ben Commented Jan 20, 2019 at 22:14
  • @kemicofa I've tried this in the node environment and it works. I will restate my question. – Duncan Gichimu Commented Jan 20, 2019 at 22:14
 |  Show 3 more ments

1 Answer 1

Reset to default 2

You are not sending headers properly. Try this.

myHeaders = new Headers({
  'Authorization': 'Token ' + process.env.API_TOKEN,
  'Content-Type': 'application/x-www-form-urlencoded'
});

and then

fetch(order_url, {
  headers: myHeaders,
  method: 'GET'
})

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744831564a4596114.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信