如何使用alamofire实现put http请求

时间:2018-04-07 09:55:55

标签: ios swift httprequest alamofire

下面我有使用此api https://jsonplaceholder.typicode.com/posts的http请求代码,我想知道如何使用alamofire来更新api上的数据,我想用id更新它

{
    "userId": 1,
    "id": 1,
    "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
    "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
  },

我的alamofire获取请求代码

func demoApi() {
        Alamofire.request("https://jsonplaceholder.typicode.com/posts", method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in

            switch(response.result) {
//            case .success(_):
//                guard let json = response.result.value as! [[String:Any]]? else{ return}
//                print("Response \(json)")
//                for item in json {
//
//                    if let name = item["name"] as? String {
//                        self.titleArray.append(name)
//                        print("ang name:" , name)
//
//                    }
//
//                    DispatchQueue.main.async {
//                        self.collectionView.reloadData()
//                    }
//                }
//                break

            case .success(_):
                guard let json = response.result.value as! [[String:Any]]? else{ return}
                print("Response \(json)")
                for item in json {

                    self.getAllDetail.append(item)

                    // if let title = item["title"] as? String {
                    //   self.titleArray.append(title)
                    // }

                }
                if !self.getAllDetail.isEmpty{
                    DispatchQueue.main.async {
                        self.collectionView.reloadData()
                    }
                }
                break

            case .failure(_):
                print("Error")
                break

            }
        }

    }

2 个答案:

答案 0 :(得分:1)

确实如此,您可以从here

获取样本
  let  params  = ["userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"] as [String: Any]


Alamofire.request("https://jsonplaceholder.typicode.com/posts", method: .put, parameters: params, encoding: JSONEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in

            switch(response.result) {


            case .success(_):
                guard let json = response.result.value as! [[String:Any]]? else{ return}
                print("Response \(json)")


                break

            case .failure(_):
                print("Error")
                break

            }
        }

    }

答案 1 :(得分:0)

供将来参考:

  let parameters = ["userId": 1,
                  "id": 1,
                  "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
                  "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"] as [String : Any]



let urlString = "https://jsonplaceholder.typicode.com/posts"

Alamofire.request(urlString, method: .post, parameters: parameters,encoding: JSONEncoding.default, headers: nil).responseJSON {
response in
switch response.result {
case .success:
print(response)

break
case .failure(let error):

print(error)
}
}