我正在尝试创建一个我的ios应用程序所连接的api。但是,当我运行发布请求并将参数设置为error: not found
时,我会继续提示id, name, birth and country
。但是,如果我只是在邮递员中运行url/users/14253/test/10-20-1992/denmark
它工作正常,所以我有点困惑我做错了什么?
api route
router.post('/:id/:name/:birth/:country', function(req, res, next) {
var params = req.params;
var accessHeader = req.headers;
var user = new User({
_id: params.id,
name: params.name,
birthday: new Date(params.birth),
country: params.country
});
user.save(function(err) {
if (err) {
res.send(err);
} else {
res.json({ message: 'User created!' });
}
});
});
swift alamofire请求
func passUserData(id: String, name: String, birth: String, country: String, token: AccessToken) {
let headers = [
"Content-Type": "application/x-www-form-urlencoded"
]
Alamofire.request("http://url/users", method: .post, parameters: ["id" : id, "name" : name, "birth" : birth, "country" : country], encoding: JSONEncoding.default, headers: headers).responseString { closureResponse in
if String(describing: closureResponse.result) == "SUCCESS"
{
print(closureResponse)
} else {
print(closureResponse)
}}
}
答案 0 :(得分:2)
您可以传递网址中的参数:
Alamofire.request("http://172.20.10.2:3000/users/" + id + "/" + name + "/" + birth + "/" + country, ...);
您可以传递正文中的参数:
router.post('/users', function(req, res, next) {
var body = req.body;
var accessHeader = req.headers;
var user = new User({
_id: body.id,
name: body.name,
birthday: new Date(body.birth),
country: body.country
});
user.save(function(err) {
if (err) {
res.send(err);
} else {
res.json({ message: 'User created!' });
}
});
});