我正在尝试通过我的快速应用程序连接到第三方端点。使用特定的主体在curl中直接向该端点执行相同的请求将使我得到400状态,并带有特定的错误消息。
现在使用express + node-fetch,我收到错误消息,但得到200状态代码。
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;
}
在路由器中,我有
router.post('/auth', handleErrorAsync(async(req: any, resp: Response, err: any) => {
let res = await authenticate();
resp.send(res);
}));
一个临时解决方案是在声明json
变量后将其添加到异步函数中。但是我觉得这应该开箱即用。
if (result.status !== 200) {
throw createError(result.status, json.error);
}