API Response always respond with 200 despite server respond with 400 in postman
Unanswered
Japanese anchovy posted this in #help-forum
Japanese anchovyOP
Each time a make an api request to /api/X it always respond with 200 even when I purposely send a bad request for testing purposes I always get 200.
I tried making the api call in postman the backend is working fine and respond with 400. Here is my code:
I tried making the api call in postman the backend is working fine and respond with 400. Here is my code:
try {
const res = await axios({
method: "GET",
url: LEADS,
headers: {
Authorization: `Bearer ${adminJWT}`,
},
data: body,
});
// WHY IT RETURNS 200 DESPITE SERVER RESPONSE IS 400 ??
return NextResponse.json({ ...res?.data }, { status: 200 });
} catch (error) {
const { status, name, message } = error.response.data.error;
return NextResponse.json({ name, message }, { status });
}1 Reply
Shy Albatross
you're expecting axios to throw on 400, does it do that? docs are pretty bad at explaining that, but my takeaway from docs is that if you don't provide a custom
https://axios-http.com/docs/handling_errors
validateStatus then it should behave just like fetch, which is not throw on HTTP errors. Or at least not below 500? I don't know, docs are p. bad.https://axios-http.com/docs/handling_errors