【发布时间】:2017-10-09 23:47:52
【问题描述】:
我有这样的控制器 -
def create
if (@user = User.find_by_email(params[:email])) && @user.valid_password?(params[:password])
render json: @user.as_json(only: [:email,:authentication_token]),status: :created
else
render json:('Unauthorized Access')
end
end
当我使用 Postman 提出此请求时,我选择正文,并形成数据并添加电子邮件和密码。这个作品
如何使用 swift 来做同样的事情?这是我尝试过的
let url = URL(string: "http://localhost:3000/api/v1/user_serialized/")
let config = URLSessionConfiguration.default
let request = NSMutableURLRequest(url: url!)
request.httpMethod = "POST"
let bodyData = "email=Test@test.com&password=Test1234"
request.httpBody = bodyData.data(using: String.Encoding.utf8);
let session = URLSession(configuration: config)
let task = session.dataTask(with: url! as URL, completionHandler: {(data, response, error) in
let json = JSON(data:data!)
debugPrint(json)
})
task.resume()
【问题讨论】:
-
对于 Postman 请求,无需询问如何编写 Swift 代码,因为 Postman 会为您编写 Swift 代码。