如何为登录设置API请求?

时间:2017-02-06 04:24:26

标签: ios json swift rest

我登录并注册了使用Swift创建的用户名和密码字段。我需要将它附加到下面的API中。当我点击注册时,它应该创建一个新用户。当我登录时,它应该创建并存储访问令牌。登录或注册后,我将进入tableview,显示我自己(当前用户)以及tableView中的所有其他用户。我可以编辑或删除tableView中的用户。我该怎么做呢?有关这方面的信息非常有用,因为我对REST API的了解有限。例如,这是一个伪URI。谢谢!

API参考

URI相对于:https://myloginapi.com/api/v1 - 模拟URI

auth.token

用户

Createaccess_token并返回。

请求
| Param        | Value                                                |
| ------------ | ---------------------------------------------------- |
| Path         | `POST` /auth/access_token                            |
| Headers      | ~~(empty)~~                                          |
| Body         | `JSON` {"name":"`(username)`","pass":"`(password)`"} |
响应
| Param        | Value                                                |
| ------------ | ---------------------------------------------------- |
| Body         | `JSON` {"data":"`access_token`"}                     |

user.get

Get一位用户id并返回其数据。

请求
| Param        | Value                                                |
| ------------ | ---------------------------------------------------- |
| Path         | `GET` /user/`(id)`                                   |
| Headers      | Authorization: `(access_token)`                      |
| Body         | ~~(empty)~~                                          |
响应
| Param        | Value                                                |
| ------------ | ---------------------------------------------------- |
| Body         | `JSON` {"data":`user`}                               |

user.update

{p> 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`}                               |

user.create

Create一位新用户使用request body并返回其数据。

请求
| Param        | Value                                                |
| ------------ | ---------------------------------------------------- |
| Path         | `POST` /user                                         |
| Headers      | ~~(empty)~~                                          |
| Body         | `JSON` {"name":"`(username)`","pass":"`(password)`"} |
响应
| Param        | Value                                                |
| ------------ | ---------------------------------------------------- |
| Body         | `JSON` {"data":`user`}                               |

user.delete

Delete一位用户id并返回一个空响应。

请求
| Param        | Value                                                |
| ------------ | ---------------------------------------------------- |
| Path         | `DELETE` /user/`(id)`                                |
| Headers      | Authorization: `(access_token)`                      |
| Body         | ~~(empty)~~                                          |
响应
| Param        | Value                                                |
| ------------ | ---------------------------------------------------- |
| Body         | ~~(empty)~~                                          |

user.list

List所有用户并返回一个集合。

请求
| Param        | Value                                                |
| ------------ | ---------------------------------------------------- |
| Path         | `GET` /user                                          |
| Headers      | Authorization: `(access_token)`                      |
| Body         | ~~(empty)~~                                          |
响应
| Param        | Value                                                |
| ------------ | ---------------------------------------------------- |
| Body         | `JSON` {"data":[`users`]}

2 个答案:

答案 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
            }
       }
 }