标头请求中的Angular2传递授权

时间:2018-07-17 11:27:10

标签: angular angular2-services

我试图通过向 request标头中的请求标头发送 btoa 身份登录字符串来授权用户,但是标头在内部发送授权键>请求有效载荷。 请参阅屏幕截图

enter image description here

登录服务:

import { Http, Headers, Response, URLSearchParams } from '@angular/http';

let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Authorization', loginString);

return this.http.post('http://localhost:9090/api/users/authenticate', 
{headers: headers}).map(res => res.json());    

我要在请求标题中发送授权密钥,而不是请求有效载荷

谢谢

2 个答案:

答案 0 :(得分:2)

您要在body参数中传递标题,请尝试

import { Http, Headers, RequestOptions, Response, URLSearchParams } from '@angular/http';

let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Authorization', loginString);

const options = new RequestOptions({headers: headers});

return this.http.post('http://localhost:9090/api/users/authenticate', 
null, options).map(res => res.json());  

答案 1 :(得分:0)

您正在使用POST请求。您放置标头的位置实际上是主体应放置的位置。但是,您的操作对于GET请求是正确的。

相关问题