【问题标题】:Get Headers from Post Response从帖子响应中获取标头
【发布时间】:2021-10-27 11:48:21
【问题描述】:

我正在使用 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;
        }))

【问题讨论】:

标签: angular typescript http-headers bearer-token angular10


【解决方案1】:

通过将匿名对象传递给具有observe 属性为responseoptions 参数,我能够从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 然后将标头和正文公开为属性。

【讨论】:

    猜你喜欢
    • 2017-11-06
    • 1970-01-01
    • 2017-07-28
    • 2019-03-04
    • 1970-01-01
    • 2013-05-28
    • 2016-07-25
    • 1970-01-01
    • 2015-08-31
    相关资源
    最近更新 更多