Calling response.json() when processing an error request (40x) returns a promise, not the data in the body
See original GitHub issueWhen I send an error from my restful api back to the server, I want to include a message. Flask has nice support for this and I can say:
if not token:
return {‘message’: ‘Missing token’}, 401
I can see the data in the body of the response and content-type is application/json.
In my app I’m using isomorphic-fetch and I’m saying:
fetch('...url...')
.then(response => {
if (!response.ok) {
const data = response.json();
...
Trouble is, data
always comes out being an unresolved promise. If I poke around inside using the debugger I can see my data (it’s in [[PromiseValue]]
) but I can’t get it. I’ve tried calling .then()
on it to try to resolve it but that just seems to generate another Promise.
How is this supposed to work? This code works fine when the response isn’t an error.
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Fetch JS retreive eror response - javascript - Stack Overflow
I am calling an API which could potentially return a 40x response with an associated error body which I want to use. I've...
Read more >Response.json() - Web APIs - MDN Web Docs
It returns a promise which resolves with the result of parsing the body text as JSON . Note that despite the method being...
Read more >How to get the response json on HTTP error 400+ ? · Issue #203
Suppose I create a request to http://example.com/404 , and the response is 404 status code with a json response like this: { "type":...
Read more >The JavaScript fetch() method | Go Make Things
The Body.text() method gets the body data as a text string, while the Body.json() method gets it as a JSON object. Both return...
Read more >Async JavaScript with Promises - Turing: Front-End Curriculum
Promises allow us to kick off an asynchronous process in the background and ... the body to a JSON data structure with response.json()...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop 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
Top GitHub Comments
@bggolding
response.json()
returns a Promise according to the specs that resolves with the deserialized data.It is specified in the
Body
Mixin that is implemented by theRequest
as well as theResponse
object. See https://developer.mozilla.org/en-US/docs/Web/API/Body/json and https://fetch.spec.whatwg.org/#body-mixin for details.I would expect
data
to always be a Promise. Could you show the code that runs whenresponse.ok
evaluates totrue
? It should not work if you handle it the same way.A working example would be:
I agree with @trixn86’s diagnosis …