我登录并注册了使用Swift创建的用户名和密码字段。我需要将它附加到下面的API中。当我点击注册时,它应该创建一个新用户。当我登录时,它应该创建并存储访问令牌。登录或注册后,我将进入tableview,显示我自己(当前用户)以及tableView中的所有其他用户。我可以编辑或删除tableView中的用户。我该怎么做呢?有关这方面的信息非常有用,因为我对REST API的了解有限。例如,这是一个伪URI。谢谢!
URI相对于:https://myloginapi.com/api/v1
- 模拟URI
Create
新access_token
并返回。
| Param | Value |
| ------------ | ---------------------------------------------------- |
| Path | `POST` /auth/access_token |
| Headers | ~~(empty)~~ |
| Body | `JSON` {"name":"`(username)`","pass":"`(password)`"} |
响应
| Param | Value |
| ------------ | ---------------------------------------------------- |
| Body | `JSON` {"data":"`access_token`"} |
Get
一位用户id
并返回其数据。
| Param | Value |
| ------------ | ---------------------------------------------------- |
| Path | `GET` /user/`(id)` |
| Headers | Authorization: `(access_token)` |
| Body | ~~(empty)~~ |
响应
| Param | Value |
| ------------ | ---------------------------------------------------- |
| Body | `JSON` {"data":`user`} |
Update
一位用户id
使用request body
并返回其数据。
请求
| Param | Value |
| ------------ | ---------------------------------------------------- |
| Path | `PUT` /user/`(id)` |
| Headers | Authorization: `(access_token)` |
| Body | `JSON` {"name":"`(username)`","pass":"`(password)`"} |
响应
| Param | Value |
| ------------ | ---------------------------------------------------- |
| Body | `JSON` {"data":`user`} |
Create
一位新用户使用request body
并返回其数据。
| Param | Value |
| ------------ | ---------------------------------------------------- |
| Path | `POST` /user |
| Headers | ~~(empty)~~ |
| Body | `JSON` {"name":"`(username)`","pass":"`(password)`"} |
响应
| Param | Value |
| ------------ | ---------------------------------------------------- |
| Body | `JSON` {"data":`user`} |
Delete
一位用户id
并返回一个空响应。
| Param | Value |
| ------------ | ---------------------------------------------------- |
| Path | `DELETE` /user/`(id)` |
| Headers | Authorization: `(access_token)` |
| Body | ~~(empty)~~ |
响应
| Param | Value |
| ------------ | ---------------------------------------------------- |
| Body | ~~(empty)~~ |
List
所有用户并返回一个集合。
| Param | Value |
| ------------ | ---------------------------------------------------- |
| Path | `GET` /user |
| Headers | Authorization: `(access_token)` |
| Body | ~~(empty)~~ |
响应
| Param | Value |
| ------------ | ---------------------------------------------------- |
| Body | `JSON` {"data":[`users`]}
答案 0 :(得分:1)
使用Alamofire。
示例请求将是:
Alamofire.request("https://myloginapi.com/api/v1").responseJSON { response in
print(response.request) // original URL request
print(response.response) // HTTP URL response
print(response.data) // server data
print(response.result) // result of response serialization
if let JSON = response.result.value {
print("JSON: \(JSON)")
}
}
有关详情,请查看他们的文档:https://github.com/Alamofire/Alamofire
答案 1 :(得分:0)
您可以使用URLSession
let urlconfig = URLSessionConfiguration.default
urlconfig.timeoutIntervalForRequest = 20
urlconfig.timeoutIntervalForResource = 60
let session = Foundation.URLSession(configuration: urlconfig, delegate: self, delegateQueue: OperationQueue.main)
let request = NSMutableURLRequest(url: URL(string:"https://myloginapi.com/api/v1")) // Set Url here
request.httpMethod = "POST" //(can set GET/POST)
request.cachePolicy = URLRequest.CachePolicy.reloadIgnoringCacheData
let paramString = String(format:"name=%@&pass=%@",username,password) //Set Parameter string here
request.httpBody = paramString.data(using: String.Encoding.utf8)
task = session.dataTask(with: request as URLRequest) {
(data, response, error) in
do {
if data == nil
{
//Failure
}
else
{
if let json = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary {
print(json) // Json response from server
}
}
}