How to get the response json on HTTP error 400+ ?
See original GitHub issueSuppose I create a request to http://example.com/404, and the response is 404 status code with a json response like this:
{
"type": "error",
"message": "What you were looking for isn't here."
}
How can I get the above json using fetch?
Issue Analytics
- State:
- Created 8 years ago
- Comments:15 (2 by maintainers)
Top Results From Across the Web
How to get the response json on HTTP error 400 in Java
http post request (json data) · get an error message from response. If you only want to use HttpsURLConnection .Like @Smile says, use...
Read more >HTTP status and error codes for JSON | Cloud Storage
HTTP status and error codes for JSON · 302—Found · 303—See Other · 304—Not Modified · 307—Temporary Redirect · 308—Resume Incomplete · 400—Bad...
Read more >How to get JSON response body if status is 400 Bad Request
A 400 error is usually a malformed request. In this case, the error looks like a business rule violation. Either read the 3rd...
Read more >Response codes - The REST API basics - Akeneo API
All the responses you can get when requesting via the REST API ... HTTP/1.1 400 Bad Request { "code": 400, "message": "Invalid json...
Read more >JSON getting 400 error in Rest API call - MuleSoft Help Center
I am getting a response code 400 error. HTTP(JSON/POST) -> Dataweave-> setpayload-> HTTP call with header application/json. getting 422 when i try the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Here is the code similar to @lasergoat, but handles json regardless of the
response.status. Also in case of network error, rejects with a custom error objectHey, just want to let you know, I settled on a fair way of doing what I needed. I thought I’d post just in case anyone else ends up in a situation similar to me where they couldn’t use a catch because fetch gives a ReadableByteStream instead of a json object.
This solution keeps your code clean and semantic. You use it like this: