如何在 Alamofire 中使用 PUT 请求

Tit*_*tus 5 put ios swift alamofire

我是 swift 的新手,我也尝试使用 Alamofire 从 API 调用数据。我对如何使用PUT 请求来更新数据感到非常困惑。我已经在这里阅读了一些解决方案,但我不知道如何在我的应用程序上应用。我正在创建一个事件应用程序,场景应该是,当参与者单击“签入”按钮时,它将更新registered_flagtrue意味着参与者将标记为“已注​​册”,并且该按钮将更改为“签出”。我真的不知道我的API服务是否正确。希望你能帮助我。太感谢了。

事件参与者的 JSON 一旦 checkInOutButton 就应该更新 Registered_flag 中的位置

{
    "event_name": "Q & A",
    "event_participants": [
        {
            "participant_id": "70984656-92bc-4c36-9314-2c741f068523",
            "employee_number": null,
            "last_name": "Surname",
            "first_name": "FirstName",
            "middle_name": null,
            "display_name": "Surname, FirstName ",
            "department_name": "Medical Informatics",
            "position_name": "Application Developer",
            "registered_flag": true,
            "registered_datetime": "2018-09-13T08:54:40.150",
            "registration_type": 1,
            "delete_flag": false,
            "manual_reg_flag": false,
            "out_flag": false,
            "out_datetime": null,
            "classification": 6,
            "others": "Guest"
          }
        }
Run Code Online (Sandbox Code Playgroud)

用于更新以进行签入的 JSON

{
   "registered_flag": true,
   "registration_type": 1
}
Run Code Online (Sandbox Code Playgroud)

更新类型

enum UpdateParticipantType: String {

case checkIn = "Check In"
case checkOut = "Check Out"
}
Run Code Online (Sandbox Code Playgroud)

UpdateParticipant 的 API 服务

 func updateParticipant(updateType: UpdateParticipantType,
                       participantID: String,
                       successBlock: @escaping ([Attendee]) -> Void,
                       failureBlock: @escaping (Error) -> Void)

{

   let updateParticipantURL = URL(string: "\(REGISTER_PARTICIPANT_URL)/\(updateType)/\(participantID)")

    Alamofire.request(updateParticipantURL!, method: .put).responseJSON { (response) in
        print(response)

        if let error = response.error
        {
            failureBlock(error)
            print(error)
            return
        }

        if let jsonArray = response.result.value as? [[String : Any]] {
            for anItem in jsonArray {
                if let eventparticipants = anItem["event_participants"] as? [[String : Any]] {
                    var extractedAttendees = [Attendee]()
                    for participants in eventparticipants{
                        let success = Attendee.init(JSON: participants)
                        extractedAttendees.append(success!)
                    }
                    successBlock(extractedAttendees)
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

ino*_*key 1

根据Alamofire文档:

let parameters: Parameters = [
    "foo": "bar",
    "baz": ["a", 1],
    "qux": [
        "x": 1,
        "y": 2,
        "z": 3
    ]
 ]

Alamofire.request("https://httpbin.org/post", method: .post, parameters: parameters)
Run Code Online (Sandbox Code Playgroud)

对于给定的 json

 {
    "registered_flag": true,
    "registration_type": 1
 }

let parameters: Parameters = [
     "registered_flag": true
     "registration_type": 1
 ]
Run Code Online (Sandbox Code Playgroud)