I am trying to connect to a third party endpoint from my express app. With specific body Doing the same request in curl directly to that endpoint gives me a 400 status with a specific error message.
Now using express + node-fetch, I am getting the error message, but I get 200 status code.
export async function authenticate() {
const result = await fetch(`${config.host}/token`, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: JSON.stringify({
key: "someValueThatShouldReturnError"
})
});
console.log(result.status); // this prints 400
const json = await result.json();
return json;
}
And in the router I have
router.post('/auth', handleErrorAsync(async(req: any, resp: Response, err: any) => {
let res = await authenticate();
resp.send(res);
}));
a temporary solution is to add this in the async function after declaring json
variable..But I feel this should be handled out of the box.
if (result.status !== 200) {
throw createError(result.status, json.error);
}