如何在Alamofire中使用PUT请求 [英] How to use PUT request in Alamofire

查看:484
本文介绍了如何在Alamofire中使用PUT请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手,我也试图使用Alamofire从API调用数据。对于如何使用 PUT请求更新数据,我感到很困惑。我已经在此处阅读了一些解决方案,但我不知道如何将其应用于我的应用程序。我正在创建一个事件应用,场景应该是,当参与者单击签入按钮时,它将把 registered_flag 更新为 true 表示参与者将被标记为已注册,并且按钮将更改为签出。我真的不知道我的API服务是否正确。希望你能帮助我。非常感谢。

I am new in swift and I am also trying to use Alamofire to call data from API. I am quite puzzled on how I will use the PUT Request to update data. 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. I really don't know if my API Service is correct or not. Hope you could help me. Thank you so much.

事件参与者的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以更新以进行签入

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

updateType

enum UpdateParticipantType: String {

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

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


推荐答案

按照 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)

对于给定的json

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

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

这篇关于如何在Alamofire中使用PUT请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆