Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular为Get请求、cURL、Python和fetch工作生成429状态代码_Angular_Imgur_Http Status Code 429 - Fatal编程技术网

Angular为Get请求、cURL、Python和fetch工作生成429状态代码

Angular为Get请求、cURL、Python和fetch工作生成429状态代码,angular,imgur,http-status-code-429,Angular,Imgur,Http Status Code 429,我正在开发一个足够简单的网站,需要对Imgur的API执行一个get请求。使用以下工具可以很好地工作: curl -v --location --request GET 'https://api.imgur.com/3/album/WFWR56Z/images' \ --header 'Authorization: Client-ID {{clientId}}' 下面的Python 3代码也是如此: import requests url = "https://api.imgur.

我正在开发一个足够简单的网站,需要对Imgur的API执行一个get请求。使用以下工具可以很好地工作:

curl -v --location --request GET 'https://api.imgur.com/3/album/WFWR56Z/images' \
--header 'Authorization: Client-ID {{clientId}}'
下面的Python 3代码也是如此:

import requests

url = "https://api.imgur.com/3/album/WFWR56Z/images"
headers = {
  'Authorization': 'Client-ID {{clientId}}'
}

response = requests.request("GET", url, headers=headers, data=payload)
设置一个简单的示例,并使用Apache和fetch在本地主机上为其提供服务:

    var myHeaders = new Headers();
    myHeaders.append("Authorization", "Client-ID {{clientId}}");

    var requestOptions = {
      method: 'GET',
      headers: myHeaders,
      redirect: 'follow'
    };

    fetch("https://api.imgur.com/3/album/WFWR56Z/images\n", requestOptions)
      .then(response => response.text())
      .then(result => console.log(result))
      .catch(error => console.log('error', error));
但是,当我像这样使用Angular的HttpClient时:


  constructor(private http: HttpClient) { }

  getImageLinks(): void {
    const headers = new HttpHeaders(
      {
        'Authorization': "Client-ID {{clientId}}",
      });
    this.http.get("https://api.imgur.com/3/album/WFWR56Z/images", { headers }).subscribe(data => {
      console.log(data);
    })
  }
}

我几乎总是得到一个429速率限制错误。很少情况下不会出现这种情况,但我似乎无法确定它何时以及为什么会返回。

多亏了@Clashsoft,我将查询作为cURL命令下载,并逐个删除了标题以查找有问题的标题:

-H'参考:http://localhost:4200/“

我使用以下方法修复了该问题:

具体来说,我补充道
到我的
index.html

角度http调用是通过浏览器完成的,浏览器会添加自己的标题和内容。检查dev工具中的network选项卡,也许将请求复制为curl,然后看看这会把你引向何方。感谢@Clashsoft的帮助,我在下面给出了答案。