-1

I have a post method using Angular's HttpClient.

I'm trying to subscribe to the response so I can do a few things after but I'm getting this error:

Error: Uncaught (in promise): HttpErrorResponse: {"headers":{"normalizedNames":{},"lazyUpdate":null},"status":200,"statusText":"OK","url":"XXXXXXX","ok":false,"name":"HttpErrorResponse","message":"Http failure during parsing for XXXXXXXX","error":{"error":{},"text":"OK"}}

I've seen somewhere that this could be because it's not a valid JSON response, when I test it on Postman, I get a OK but not as JSON.

My question is, how do I work around this? Is there a way for me to convert this to JSON?

My method looks like:

submitInfo() {
   this.http.post(url, data).toPromise()
      .then(
          (response: Response) => {
              console.log(response);
          }
      ));
}
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
MrRobot
  • 1,001
  • 1
  • 14
  • 34

1 Answers1

1

By default Angular tries to process HTTP responses as JSON and hence it tends to go into error handler even though the actual HTTP request succeeds. For non-JSON responses you can explicitly specify that you are expecting a text response in your HTTP request or to get the benefits of inbuilt Angular error handling for responses modify your response from server/middleware as a JSON.

For non-JSON response try modifying your HTTP request like below

this.http.post(url, parameters, { responseType: 'text' });
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
RR1112
  • 226
  • 1
  • 9
  • @jonrsharpe thank you! So when you explicit say the `responseType: 'text'` this is not to do a transformation right? rather is to literally tell what do expect? – MrRobot Oct 01 '19 at 14:44
  • 1
    @iconio it tells it what to expect, which *also* disables the default `JSON.parse` transformation. – jonrsharpe Oct 01 '19 at 14:46
  • Thank you very much brother! I really really appreciate your help – MrRobot Oct 01 '19 at 14:50