角没有过载匹配此调用

时间:2019-09-07 08:10:50

标签: angular http-headers

我试图使用Angular从flightaware获取数据Json api。并且来自flightaware的所有请求都必须包括用户名和使用基本HTTP身份验证标准的FlightXML密钥。

代码

  var fxml_url = 'http://flightxml.flightaware.com/json/FlightXML2/';
  var username = 'YOUR_USERNAME';
  var apiKey = 'YOUR_APIKEY';


  this.http.get(fxml_url + 'MetarEx', {
      username: username,
      password: apiKey,
      query: {airport: 'KAUS', howMany: 1}
  }).subscribe(data=>{

    console.log(data)

  })

但是我得到的错误是

No overload matches this call.
  The last overload gave the following error.
    Argument of type '{ username: string; password: string; query: { airport: string; howMany: number; }; }' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }'.
      Object literal may only specify known properties, and 'username' does not exist in type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }'.

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

来自docs:但是,对于某些库,可能有必要在base64中手动编码“ user:key”,并将结果作为每个HTTP请求的一部分发送到“ Authorization”标头中

var fxml_url = 'http://flightxml.flightaware.com/json/FlightXML2/';
var username = 'YOUR_USERNAME';
var apiKey = 'YOUR_APIKEY';
var auth = btoa(`${username}:${apiKey}`);

this.http.get(fxml_url + 'MetarEx', {
  headers: { Authorization: auth },
  params: new HttpParams({fromObject: { airport: 'KAUS', howMany: '1' }})
}).subscribe(data=>{
  console.log(data)
});