API中的Put方法总是接收空的请求主体



我已经构建了一个NodeJS Restful API,除了一个路由之外,几乎每个路由都在工作。我有一个Angular前端,使用httpClientModule发送请求。

当我尝试为一个特定项目发出put请求时,该请求在到达服务器时为空。我有3条其他路线提出看跌请求,它们都运行良好。有人能解释一下这里发生了什么吗。我看过其他一些讨论类似问题的帖子,但没有一个问题与我的相同。我真的无法理解发生了什么。

这是一些相关的代码。提前谢谢。

API方法

router.put("/api/login/:_id", passport.authenticate("jwt", { session: false }), (req, res) => {
console.log(req.body)
User.findById(req.params._id)
.then((user) => {
bcrypt.compare(req.body.password, user.password).then((isMatched) => {
if (isMatched) {
User.findByIdAndUpdate(_id, { password: req.body.password })
.then(() => {
res.sendStatus(200);
})
.catch((err) => {
console.log("Password update error" + err);
});
} else {
res.status(400);
}
});
})
.catch((err) => {
console.log("Password Update Error" + err);
res.status(400);
});
});

角度服务方法

newPassword(_id: any, password:any):Observable<any>
{    
return this.http.put(this.url+`/admin/api/login/${_id}`, password);
}

如果您在请求中将密码体封装到对象中,该怎么办?比方说:

newPassword(_id: any, password: any): Observable < any >
{
return this.http.put(this.url + `/admin/api/login/${_id}`, {password});
}

通过这种方式,主体包含一个password属性。

最新更新