While using Angular HttpClient post, it seems the default is to treat the response as JSON string. This caused error even for 201 response when the response body is empty as empty string "" fails JSON.parse().
The solution is to specify responseType: "text" as additional option, so that empty body is not treated as error.
However, the API endpoint does return error description in JSON when the request fails (i.e. empty on success, JSON on error).
How do you construct the HttpClient post so that I can get the error message object back when it fails and success does not count as error?
e.g.:
.subscribe(() => {
// do something for success, no return object as the body is empty
, error => {
// do something with the returned error object
// right now success is still counted as an error due to the issue above
}
);
201or200but some other code, like422for validation errors or500for server error. For an empty response, but no error, it should be204. Could you try with that? I have a few angular apps that send requests to a few different APIs and I never saw this problem before.{}. But is there a way to workaround them on the client side as I cannot update the API endpoints.