简体   繁体   English

如何获取响应头?

[英]how to get response headers?

I am using angular 15.0.我正在使用 angular 15.0. I get a list of items from backend (asp.net core 5) with an item appended to the header. the get method in the client side service is:我从后端(asp.net core 5)获得了一个项目列表,其中一个项目附加到 header。客户端服务中的 get 方法是:

 /** GET Paged commodities from the server ===============================*/ getPagedCommodities(pageSize: number, pageNumber: number): Observable<CommodityForList[]> { let params: HttpParams = new HttpParams(); params = params.append('pageSize', pageSize); params = params.append('pageNumber', pageNumber); const headers = new HttpHeaders({ observe: 'response' }); return this.http.get<CommodityForList[]>(this.baseUrl + '/getPagedCommodities/', { headers, params }); }

this method sends two parameters to the server and gets commodities list along with totalCount to paging the list.该方法向服务端发送两个参数,获取商品列表和totalCount ,对列表进行分页。 I need the totalCount to set the length property of mat-paginator and commodities list as observable to provide a dynamic search for user.我需要totalCountmat-paginator和商品列表的length属性设置为可观察的,以便为用户提供动态搜索。 therefore, in the commoditiesList component, the snippet code for this purpose is:因此,在commoditiesList组件中,用于此目的的代码片段是:

 commoditiesForList$:; Observable<CommodityForList[]>. this.commoditiesForList$ = this.commoditiesService.getPagedCommodities(this,pageSize. this;pageIndex+1). this.commoditiesForList$.subscribe( res => { const totalCount = res.headers;get('X-Pagination'); })

but, here I have an error: Property 'headers' does not exist on type 'CommodityForList[]'.但是,这里我有一个错误: Property 'headers' does not exist on type 'CommodityForList[]'. when I change the type of commoditiesForList$ to HttpResponse<CommodityForList[]> , the error may be fixed, but receiving the commodities list as observable will have a problem.当我将commoditiesForList$的类型更改为HttpResponse<CommodityForList[]>时,错误可能已修复,但接收商品列表作为可观察的将有问题。 Is there a solution to get the commodities list as observable and read the totalCount separately from the header?是否有一种解决方案可以将商品列表作为可观察的并从 header 中单独读取totalCount thank you for your response.感谢您的答复。

See https://angular.io/guide/http#reading-the-full-response参见https://angular.io/guide/http#reading-the-full-response

You might need more information about the transaction than is contained in the response body.您可能需要比响应正文中包含的更多有关交易的信息。 Sometimes servers return special headers or status codes to indicate certain conditions that are important to the application workflow.有时服务器会返回特殊的标头或状态代码,以指示对应用程序工作流很重要的某些条件。

  getPagedCommodities(pageSize: number, pageNumber: number): Observable<HttpResponse<CommodityForList[]>> {
    // ...
    return this.http.get<CommodityForList[]>(this.baseUrl + '/getPagedCommodities/', { headers, params });
  }

You can access the body in the body attribute.您可以在body属性中访问 body。

See maybe also Angular: HttpClient read full response with Observable Array .也可以参见Angular:HttpClient 使用 Observable Array 读取完整响应

Given your example, you could use the next configuration on your HTTP request: {responseType: 'json', observe: 'events' } .根据您的示例,您可以在 HTTP 请求中使用下一个配置: {responseType: 'json', observe: 'events' } See a working example here on stackblitz - one request请参阅此处关于stackblitz 的工作示例 - 一个请求

commoditiesForList$!: BehaviourSubject<CommodityForList[]>;
totalCount$!: BehaviourSubject<any>;

constructor(commoditiesService: CommoditiesService) {
this.commoditiesService.getPagedCommodities(this.pageSize, this.pageIndex+1).subscribe(res => {
  this.commoditiesForList$.next(res.body)
  this.totalCount$.next(headers.get('X-Pagination'))
})
}

Original Answer原始答案

Given your example, you could use the next configuration on your HTTP request: {responseType: 'json', observe: 'events' } .根据您的示例,您可以在 HTTP 请求中使用下一个配置: {responseType: 'json', observe: 'events' } See a working example here on stackblitz - two requests - shared pipe请参阅此处关于stackblitz 的工作示例 - 两个请求 - 共享 pipe

Edit: to avoid making two request, notice that GET request is using share operator from rxjs. Thanks Arber to notice it.编辑:为避免发出两个请求,请注意 GET 请求使用来自 rxjs 的共享运算符。感谢 Arber 注意到它。

getPagedCommodities(pageSize: number, pageNumber: number): Observable<CommodityForList[]> {
    let params: HttpParams = new HttpParams();
    params = params.append('pageSize', pageSize);
    params = params.append('pageNumber', pageNumber);

    const headers = new HttpHeaders({
      observe: 'response'
    });

    return this.http.get<CommodityForList[]>(this.baseUrl + '/getPagedCommodities/',
    { headers, params, responseType: 'json',observe: 'events'}).pipe(share());

  }

then you will access data and headers in this way那么您将以这种方式访问数据和标题

 commoditiesForList$!: Observable<CommodityForList[]>;
 
this.commoditiesForList$ = this.commoditiesService.getPagedCommodities(this.pageSize, this.pageIndex+1).pipe(
    map((res) => (res as any).body));

this.totalCount$ =  this.commoditiesService.getPagedCommodities(this.pageSize, this.pageIndex+1).pipe(
    map((res) => (res as any).headers)), map(headers => headers.get('X-Pagination')));

Docs : https://angular.io/guide/http#requesting-data-from-a-server文档https://angular.io/guide/http#requesting-data-from-a-server

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

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