简体   繁体   English

如何在Alamofire中使用PUT请求

[英]How to use PUT request in Alamofire

I am new in swift and I am also trying to use Alamofire to call data from API. 我是新手,也尝试使用Alamofire从API调用数据。 I am quite puzzled on how I will use the PUT Request to update data. 我对如何使用PUT请求更新数据感到非常困惑。 I've read some solutions here in SO but I don't know how I will apply on my app. 我在这里已经阅读了一些解决方案,但是我不知道如何将其应用于我的应用程序。 I am creating an Event App, the scenario should be, when the participant clicked the Check In Button, it will update the registered_flag to true meaning the participant will marked as Registered and the button will be changed to Check Out . 我正在创建一个Event App,该场景应该是:当参与者单击“ 签入”按钮时,它将更新registered_flagtrue这意味着该参与者将被标记为“已注册”,并且该按钮将更改为“ 签出” I really don't know if my API Service is correct or not. 我真的不知道我的API服务是否正确。 Hope you could help me. 希望你能帮助我。 Thank you so much. 非常感谢。

JSON of the Event Participant Where in the registered_flag should be updated once checkInOutButton 事件参与者的JSON,一次在registered_flag中更新的地方checkInOutButton

{
    "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"
          }
        }

JSON to update for check in JSON更新以签入

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

updateType UPDATETYPE

enum UpdateParticipantType: String {

case checkIn = "Check In"
case checkOut = "Check Out"
}

APIService for UpdateParticipant 适用于UpdateParticipant的APIService

 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)
                }
            }
        }
    }
}

As per Alamofire documentation: 根据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)

For a given json 对于给定的json

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

let parameters: Parameters = [
     "registered_flag": true
     "registration_type": 1
 ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM