我是斯威夫特的新手。我想对用户进行身份验证。我使用“已部署”作为我的服务器。这就是 API 的文档所说的:
HTTP
要对用户进行身份验证,请向 /login 发送一个 POST 请求,请求正文中包含用户名和密码属性。
发布/用户/登录
{“用户名”:“约翰史密斯”,“密码”:“密码”}
我正在使用 Alamofire 解析 JSON 数据。这是我的代码:
let user = "root"
let password = "root"
// this is a testing user i've created in deployd's dashboard.
let credential = NSURLCredential(user: user, password: password, persistence: .ForSession)
Alamofire.request(.POST, "http://localhost:2406/users/login/\(user)/\(password)")
.authenticate(usingCredential: credential)
.response { request, response, _, error in
println(response)
}
这是来自 Xcode 控制台的响应:
Optional(<NSHTTPURLResponse: 0x7fdd906dc3a0> { URL: http://localhost:2406/users/login/root/root } { status code: 400, headers {
Connection = "keep-alive";
"Content-Type" = "application/json";
Date = "Thu, 06 Aug 2015 13:01:54 GMT";
"Transfer-Encoding" = Identity; } })
我知道我这样做的方式完全错误。