-
Notifications
You must be signed in to change notification settings - Fork 94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
onResolve doesn't trigger if response is empty #235
Comments
Hi, That's unexpected behavior. Can you try setting an onReject handler as well, and see if that gets invoked when you do I tried setting up an Express route like that myself: app.use('/foo', (req, res) => {
return res.sendStatus(200);
}); When requested from the browser with
and |
My
I tried using an the onReject callback as well and got
in this line |
Changing my Accept header to something like: Maybe the best solution would simply be a better error on the onReject and/or a mention in the docs ? |
As your Try running Either your server needs to return valid JSON or your headers should not indicate that the server should return JSON. I'd say all of this is exactly expected behaviour: the server is returning invalid JSON although you specificly requested JSON from the server. |
@phryneas: This makes a lot of sense and I realise my mistake :) What's still confusing, and why I think
|
Yes, but Realistically, when using fetch(`${url}`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization:
"Bearer eyJ0e..."
},
body: JSON.stringify(payload)
})
.then(res => {
console.log("will enter here even with the wrong Accept header");
+ return res.json();
+ })
+ .then(json => {
+ console.log("will never get here if it is not valid JSON");
})
.catch(e => {
console.log("error", e);
}); Because, if you never cared about the JSON return value in the first place, there's just no reason to specify the Of course, So, |
We should change the automatic json deserialization to look at Additionally, I would like to extend the json deserialization to error responses as well. Many servers return a json-encoded error message in the body for error responses, which currently is hard to retrieve with |
Nope, it's right this way. Accept for the kind of response we want to see from the server, content-type for the type of data we're sending. From MDN:
|
Of course, we could look at the content-type of the response, but that would make the server dictate behaviour of the client and I guess that would lead to all kinds of weird behaviour. We could look at the content-type of an error and, if we're waiting for JSON anyways because of the accept header or json option, use that information to decide if the error response should be deserialized. |
Could we make it so that if an empty response is returned, then For example, if a DELETE fails, then it might return JSON, but if it succeeds, it just returns an empty response. |
I suppose we could look at the Content-Length response header before trying to deserialize any JSON. I'd be happy to accept a PR for that. |
Ah, yes, that sounds like a good middle ground! Actually... perhaps an even better solution would be to only parse content as JSON when the request's |
Any thoughts on my last comment? 🙂 |
Sorry this took so long. I'm okay with requiring the server's response Care to open a PR (or two, one for each of these solutions)? |
Hello,
Thanks for the library :)
I'm looking at the useFetch hook and specifically the associated
onResolve
callback to make a call to my backend and redirect to another URL if the call is successful using this pattern:I have absolutely no problem reaching my
console.log
if I return data in my response in my Express backend like so:However, sending an empty 200 response like so:
never triggers the
onResolve
callback and theconsole.log
is never printed.Is this the intendend behavior ?
If it is, maybe editing the documentation and mentionning the required data in the response would be helpful.
I'm happy to have a look and PR if needed :)
The text was updated successfully, but these errors were encountered: