发出http请求

时间:2018-11-16 12:55:29

标签: javascript reactjs xmlhttprequest

我想在有或没有Axios的React App上发出如下所示的get请求。但是我不知道如何为此添加身份验证令牌。

curl -X GET https://server.url/acp-service/sites -H 'Content-Type: application/json' -H 'X-Authorization: eyJhbGciOiJIUzUxMiJ9.eyJ...long.string.here'

能请你帮我吗?

1 个答案:

答案 0 :(得分:1)

使用fetch()

(async () => {
  const rawResponse = await fetch(' https://server.url/acp-service/sites', {
    method: 'GET',
    headers: {
      'Content-Type': 'application/json',
      'X-Authorization': 'your_auth_string'
    }
  });
  const content = await rawResponse.json();

  console.log(content);
})();