【问题标题】:How to use multiple axios header如何使用多个axios header
【发布时间】:2020-01-20 14:41:09
【问题描述】:

我正在开发 react-native 应用程序。 该项目使用 FatSecret REST API。 此 api 使用 OAuth 2.0 的身份验证。 首先,我收到访问令牌。 我尝试使用令牌接收数据并出现错误。 发帖给我状态码 200,但我不明白。

    let form = new FormData();

    var headers = new Headers();
    headers.append("content-type", "application/json"),
      headers.append(
        "Authorization",
        "Basic " +
          base64.encode(
            <clientID>:<clientSecret>,
          ),
      ),
      form.append("grant_type", "client_credentials");
    form.append("scope", "basic");

    fetch("https://oauth.fatsecret.com/connect/token", {
      method: "POST",
      headers: headers,
      body: form,
    })
      .then(res => res.json())
      .then(json =>{this.calc(json.access_token)})


  };

//------------------------------------------------------
//success (get Access token)
//--------------------------------------------------------

  calc =(token)=>{  
    console.log(token)

    var headers2 = new Headers();
    headers2.append("Content-Type", "application/json");
    headers2.append(
      "Authorization",
      "Bearer "+token),

    fetch("https://platform.fatsecret.com/rest/server.api",{
      method :"POST",
      headers:headers2,
      body:"method=foods.search&search_expression=toast&format=xml"
    }).then(res=>console.log(res));
}

Response {type: "default", status: 200, ok: true, statusText: undefined, headers: Headers, …}
headers: Headers {map: {…}}
ok: true
status: 200
statusText: undefined
type: "default"
url: "https://platform.fatsecret.com/rest/server.api"
_bodyBlob: Blob {_data: {…}}
_bodyInit: Blob {_data: {…}}
__proto__: Object

【问题讨论】:

  • 您遇到的错误是什么?您提供的代码中的哪里遇到了错误?

标签: javascript node.js react-native axios fetch


【解决方案1】:

您可以将以下代码用于带有 axios 的多个标头:

const instance = axios.create();
      instance.defaults.headers.common['Accesstoken'] = 'Bearer '+token;

      const response = await instance.post(<api url>, {
        id: orderId
      });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-28
    • 2019-01-06
    • 1970-01-01
    • 2020-06-11
    • 2018-08-05
    • 2023-03-18
    • 1970-01-01
    • 2016-04-12
    相关资源
    最近更新 更多