为什么不能将标题添加到axios.get?

时间:2019-08-20 11:35:37

标签: api vue.js http-headers axios

我正在使用axios和vue.js来玩Fortnite Tracker API。 在他们的文档中明确指出,我们需要在标头中包含“ TRN-Api-Key”。

我在Postman上进行了测试,并且可以正常工作。

这是我发出请求的axios函数:

let url = `https://api.fortnitetracker.com/v1/profile/${this.platform}/${this.username}`;
// username and platform are from my Vue Component.
   axios.get(url, {
      headers: {
        "TRN-Api-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxx" // of course from my account on their website.
      }
   })
       .then(response => console.log(response.data))

我希望像Postman一样在json中输出,但是出现404错误:“网络错误”。

在浏览器网络调试中,我看不到请求标头'TRN-Api-Key'。

[编辑]

1 个答案:

答案 0 :(得分:1)

如果您的应用程序正在服务器上运行,则可以编写一个简短的PHP脚本并在其中使用curl来访问API(我认为甚至可以生成PHP代码(来自邮递员)。 只需使用axios解决此脚本,然后提交您的platformusername属性即可构建正确的网址。

或者也可以看看此post。也许使用受接受者答案中提供的其他API(例如@kecinotrab)也可以为您提供帮助。

相关问题