Angular 5 httpClient既有HttpHeaders又有Response对象?

时间:2018-05-08 20:49:51

标签: angular httpclient

我正在使用Angular 5和httpClient。

此代码使用HttpHeaders发送帖子。太好了!

const httpOptions = {
   headers: new HttpHeaders({
     'Content-Type': 'application/json',
     'Authorization': 'my-auth-token'
   }),
};


return this.http.post<any>(serviceUrl + url, dto, httpOptions)
  .pipe(
    catchError(this.handleError)
  );

此代码返回响应对象。太好了!

  return this.http.post<any>(serviceUrl + url, fileForUpload, { observe: 'response' })
    .pipe(
      catchError(this.handleError)
    );

我怎么能同时拥有这两个?使用HttpHeaders POST并获取响应对象?感谢

2 个答案:

答案 0 :(得分:-1)

const httpOptions = {
   headers: new HttpHeaders({
     'Content-Type': 'application/json',
     'Authorization': 'my-auth-token'
   }),
   observe: "response"
};

return this.http.post<any>(serviceUrl + url, fileForUpload, httpOptions)
.pipe(
  catchError(this.handleError)
);

答案 1 :(得分:-1)

这有效:

export interface IRequestOptions {
  body?: any;
  headers?: HttpHeaders | { [header: string]: string | Array<string> };
  observe?: any;
  params?: HttpParams | { [param: string]: string | Array<string> };
  reportProgress?: boolean;
  responseType?: "json";
  withCredentials?: boolean;
}

const options: IRequestOptions = {
  headers: new HttpHeaders({
  "Content-Type": "application/json",
  "Authorization": "my-auth-token"
}),
observe: "response"
};