简体   繁体   English

Angular HttpClientModule请求标头

[英]Angular HttpClientModule request headers

I'm trying to run an API call using the Angular HttpClientModule: 我正在尝试使用Angular HttpClientModule运行API调用:

getUser(ticket) {
console.log(ticket); // Returns valid ticket
    return this.http.get('https://myapi/api/v1/flow-analysis', {
      headers: new HttpHeaders().set('X-Auth-Token', ticket)
    });
  }

When I'm running this API call in my client I get the folowing response: 当我在客户端中运行此API调用时,得到以下响应:

'Failed to provide service ticket to validate'

I tried to run the same call in POSTMAN, and I got good response: 我试图在POSTMAN中运行相同的调用,并且得到了很好的响应:

在此处输入图片说明

I'm using Chrome with this extension . 我正在使用带有此扩展程序的 Chrome。 Not sure if it causes the problem. 不知道是否会导致问题。

Any idea what could have gone wrong? 知道发生了什么问题吗?

I'm using Angular 5 我正在使用Angular 5

You need to append the Content-Type header: 您需要附加Content-Type标头:

getUser(ticket: string) {
    console.log(ticket); // Returns valid ticket
    let headers: Headers = new HttpHeaders();
    headers.append('Content-Type', 'application/json');
    headers.append('X-Auth-Token', ticket);

    return this.http.get('https://myapi/api/v1/flow-analysis', {
        headers: headers
    });
}

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

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