Angular HttpClient Get method with body

前端 未结 4 1486
一整个雨季
一整个雨季 2020-12-15 16:57

I\'m improving an existing API, and the requirement is to provide a single get method which can accept multiple search criteria and based on those criteria perform the query

4条回答
  •  没有蜡笔的小新
    2020-12-15 17:40

    This worked for me:

    import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http'; 
    
    filterBooks(category: string, year: string): Observable {
       let httpHeaders = new HttpHeaders()
                             .set('Accept', 'application/json');
       let httpParams = new HttpParams()
                            .set('category', category)
                        .set('year', year);
       console.log(httpParams.toString());
       console.log(httpHeaders.keys());
       return this.http.get(this.bookUrl, {
                    headers: httpHeaders,
                    params: httpParams, 
                    responseType: 'json'
            });
    } 
    

提交回复
热议问题