almost every API function has this code:
if (res.ok) {
const data = await res.json();
// ... other code ...
}
the problem is that if this is called on a response from a route that has an OK response that returns a string and not json, like:
return res.status(OK).send("Card successfully verified");
an error will be thrown because the response body doesn't have json to parse. that means that errors will be thrown when the behavior was actually correct. so we can do one of two things:
either we
let data;
const contentType = response.headers.get('content-type');
if (contentType?.includes('application/json')) {
data = await response.json();
} else {
data = await response.text();
}
OR we
return res.status(OK).json({
message: "Card successfully verified"
});
almost every API function has this code:
the problem is that if this is called on a response from a route that has an OK response that returns a string and not json, like:
an error will be thrown because the response body doesn't have json to parse. that means that errors will be thrown when the behavior was actually correct. so we can do one of two things:
either we
OR we