1
我试图创建一个api,这是我的iOS应用程序连接到的。不过,当我运行post请求并将参数设置为id, name, birth and country
时,我不断收到提示error: not found
。但是,如果我只是运行邮递员url/users/14253/test/10-20-1992/denmark
它工作正常,所以我有点困惑我做错了什么?nodejs api和swift应用程序之间的连接
API路线
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!' });
}
});
});
迅速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)
}}
}
你能在你的node.js代码中打印'params'吗? – DrakaSAN
你不会在URL中传递任何参数,但在身体中。您在客户端执行的请求具有以下网址:“/ users”与参数在正文中 – krakig
在我看来,swift代码会以http:// IP:3000/users?id的形式生成url = bla&name = bla&etc,绝对不是你想要的网址。参数需要嵌入到url本身。 – Gimby