Axios POST请求负载未到达端点



我有两个Express服务器在运行。网关和身份验证服务。我正试图从网关向身份验证服务器发出请求,但我看不到任何CORS问题。然而,无论我在体内设置了什么有效载荷,它都不会到达我的身份验证服务。但如果我尝试像Postman这样的人提出同样的请求,效果会很好。这是我的身份验证服务控制器:authentication-service controller

export const post = async (req: Request, res: Response): Promise<Response> => {
const newUser = req.body;
if (newUser.email === undefined || newUser.password === undefined) {
log.debug(newUser);
log.debug('Email and password were not defined');
return res.sendStatus(400);
} else {
return res.sendStatus(200);
}
}

gateway

const data = new FormInfo();
data.append('email', 'test');
data.append('password', 'test');
console.log(data);
const registerRes = await axios({
method: 'post',
url: process.env.REGISTER_USER_URL,
headers: {
...data.getHeaders(),
},
data,
})
.then((response: any) => response)
.catch((error) => error.response);

几天来,我一直在挠头,想弄清楚这一点,有人知道我做错了什么吗?

我试过禁用CORS。

我还尝试过以这种方式启用CORS:

const whitelist = ['http://localhost:3000'];
const corsOptions = {
credentials: true,
origin: (origin: any, callback: any) => {
if (whitelist.indexOf(origin) !== -1) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
}
},
};
app.use(cors(corsOptions));

目前CORS是这样设置的:

app.use(cors());

我使用qs而不是FormInfo解决了这个问题。如果有人知道为什么会这样,请告诉我,这样我就可以更新答案。

最新更新