簡體   English   中英

從發布響應中獲取標題

[英]Get Headers from Post Response

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

通過將匿名對象傳遞給具有responseobserve屬性的options參數,我能夠從 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將標頭和正文作為屬性公開。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM