简体   繁体   English

对axios请求进行故障排除

[英]Troubleshooting axios requests

I've come across this problem a few times now and I feel like there's someone out there who knows a better way to troubleshoot api calls than i - specifically request headers. 我已经遇到过几次这个问题,我觉得那里有人比我更了解解决api调用的问题-特别是请求标头。

Often when i need to pass my api token via a request header, I am constantly groping at the exact format to pass my key's (it seems there's not a universal format for doing this). 通常,当我需要通过请求标头传递我的api令牌时,我会不断地摸索传递密钥的确切格式(似乎没有通用格式可以执行此操作)。

For example, I am currently trying to access the vultr v1 api. 例如,我当前正在尝试访问vultr v1 api。 The docs give a curl example where API-Key: SOMEKEY needs to be passed, yet, my first attempt rarely works then i'm just groping... Do they want my key in a key/value pair or a single string in an array? 该文档提供了一个curl示例,其中需要传递API-Key: SOMEKEY ,但是,我的第一次尝试很少起作用,然后我只是在摸索...他们是想要我的键在键/值对中还是在一个字符串中?阵列? Do i use es6 objects (without quotes) or not. 我是否使用es6对象(不带引号)。

here's what i mean: 这是我的意思:

// one method 
const opts = {
  headers: {
    API-Key: 'SOMEKEY'
  }
}
// another
const opts = {
  'headers': {
    'API-Key': 'SOMEKEY'
  }
}
// another
const opts = {
  headers: [
    'API-Key: SOMEKEY'
  ]
}

axios.get(url, opts).then(res => console.log(res.data))

which is the proper way? 哪种方法合适? In the curl example given by vultr it shows: curl -H 'API-Key: EXAMPLE' https://api.vultr.com/v1/iso/list 在vultr给出的curl示例中,它显示: curl -H 'API-Key: EXAMPLE' https://api.vultr.com/v1/iso/list -H'API curl -H 'API-Key: EXAMPLE' https://api.vultr.com/v1/iso/list EXAMPLE'https://api.vultr.com/v1/iso/list

I also see in my network inspector that the request headers show i am passing my API key yet i am still getting a 403 (bad key error) 我还在网络检查器中看到,请求标头显示我正在传递我的API密钥,但仍然收到403(错误密钥错误) 网络检查员

I have double checked the validity of my key and that's not the problem. 我仔细检查了密钥的有效性,这不是问题。 My question is this: How do i find the correct format for the headers? 我的问题是:如何找到标题的正确格式? Is there a better troubleshooting method for this kind of problem? 对于这种问题,是否有更好的故障排除方法? Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks ya'll 谢谢你们

UPDATE: Turns out they've got access control based on IP's. 更新:事实证明他们有基于IP的访问控制。 I hadn't noticed it till just now. 直到现在我还没有注意到它。 They were blocking my request because of this. 因此,他们阻止了我的请求。 My question still stands however. 我的问题仍然存在。 Good methods for figuring out correct formats? 找出正确格式的好方法? Is there a correct format? 是否有正确的格式?

One way is setting Headers while creating axios object, as follows : 一种方法是在创建axios对象时设置标头,如下所示:

 axios = axios.create({headers: {'API-Key': 'EXAMPLE'}});
 axios.get(url, opts).then(res => console.log(res.data))

Or 要么

axios.get(url, {headers: {'API-Key': 'EXAMPLE'}}).then(res => console.log(res.data))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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