发布请求失败了一些curl命令但没有其他人
我在节点中设置了一个post route,如下所示:
router.route('/').post((req, res) => {
pool.query('INSERT INTO users SET ?', req.body, (err, result) => {
if (err) throw err;
res.status(201).send(`User added with ID: ${result.insertId}`);
});
});
如果我使用这个curl命令,它完美地工作并添加用户:
curl -d "username=Dinesh Chugtai&[email protected]&password=test" http://localhost:4000/users
但是,当通过Postmate发送帖子请求时,我收到此错误:ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
Postmate还会生成一个curl命令,就是这个命令:
curl -X POST 'http://localhost:4000/users?username=johnsmith&[email protected]&password=password'
如果我在命令行中手动输入,它也会给我同样的错误。
为什么一个curl命令工作而另一个没有?我的邮寄路线设置正确吗?
回答如下:在Node.js路由定义中,您希望获取req.body
中的数据,这是POST请求的主体。
- 第一个
curl
起作用是因为你把数据放在体内 - 第二个失败是因为你把数据放在查询参数中(在URL本身)