我正在使用http.post
调用。net Core Web API。
我唯一的问题是我还想从HTTP响应对象中读取报头值,即承载令牌。我能做到吗?
var user = this.http
.post<User>(this.loginUrl, body, { 'headers': headers })
.pipe(map(user => {
//do stuff with user data here
return user;
}))
通过将一个匿名对象传递给options
参数,该参数具有observe
属性为response
,我能够从Web API中的POST方法访问响应中的标头:
const httpOptions: { headers; observe; } = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
}),
observe: 'response'
};
var user = this.http
.post<User>(this.loginUrl, body, httpOptions)
.pipe(map(event => {
//the checking of the HttpEventType is not strictly necessary
if (event.type == HttpEventType.Response)
{
let user = event.body;
let headers = event.headers
//do stuff with user data and headers
return user;
}
}))
HttpEvent然后将header和body作为属性公开。