Angular HttpClient : How to get raw HTTP response, 即使出错

标签 angular httpclient http-error

在使用 HttpClient 的 Angular 服务中,我创建了一个方法,该方法从 POST 返回原始 HTTP 响应。 :

httpOptions = {
  headers: new HttpHeaders({
    'Content-Type': 'application/json'
    //observe: 'response'
  }),
  observe: 'response' as 'body'
};

forgotLogin(requestData: PUMRequestData): Observable<HttpResponse<string>> {
  return this.http.post<HttpResponse<string>>("http://...", requestData, this.httpOptions);

然后在我的组件中,我只查看原始 http 响应中的状态和正文。
this.api.forgotLogin(pumRequestData).subscribe(response => {
  this.hStatus = response.status;
  this.hBody = response.body;
});

问题是如果响应状态不在200系列,然后 HttpClient引发错误。在我的用例中,我只想获取请求状态和正文,而不管状态是什么,我想根据组件中的状态决定要采取的操作,而不是在服务中的错误处理程序中。我一直试图找出一些棘手的方法来在服务中创建一个错误处理程序,该处理程序只返回(可观察的)HTTP响应,并且无法-甚至不确定这是否是正确的方法。

那么当状态不是 200 系列时,如何将原始 http 响应返回给我的组件?

最佳答案

这对我有用 HttpClient not able to retrieve plain text .

That's right.
It works when there is no generic code applied to it.
it will work for:

http.get(url, {responseType: 'text'}

关于Angular HttpClient : How to get raw HTTP response, 即使出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58209997/

相关文章:

.net - GitHub 操作发布我的站点并以 HTTP ERROR 500.30 结束

angular - 在Docker Windows容器中运行的.Net核心Angular应用

javascript - Angular 2 : ERROR TypeError: Cannot read property 'value' of undefined

angular - 当用户单击“添加记录”按钮时如何添加记录

angular - 如何以表格形式发送mat-chip-list

node.js - 我不知道为什么我的 http 请求在我的 Angular 项目中不起作用?

typescript - RXJS Angular - 使用 .subscribe 和 Observable 进行错误处理

kotlin - 克托尔。无法在 kotlin-js 端创建 Httpclient 的实例

angular - 416 (Requested Range Not Satisfiable) 使用 blob 作为视频时出现 HTTP 错误 "src"

android - J2ME上的httpconnection和Android上的HttpUrlConnection有什么区别(http错误401)