无法在Alamofire中使用.PUT请求更新API中的数据

时间:2018-09-14 06:50:14

标签: ios swift alamofire put

我正在创建一个活动应用,其中用户应点击签入按钮以注册该活动。用户点击按钮后,registered_flag : false将是trueregistered_type: 0将是1。但是在我构建和运行该应用程序时,出现了显示

的错误
  

responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error   Domain = NSCocoaErrorDomain代码= 3840“字符周围的值无效   0。“ UserInfo = {NSDebugDescription =字符0周围的值无效。}))。

对于我在APIService中代码的内容,我并不是很自信。希望有人会帮助我更正我的代码,以便我可以成功构建和运行我的应用程序。非常感谢。

需要在event_participant内更新的

application / json

{
   "registered_flag": false,
    "registration_type": 0
}

event_participant

"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": "Department",
        "position_name": "Developer",
        "registered_flag": false,
        "registered_datetime": "2018-09-13T08:54:40.150",
        "registration_type": 0,
        "delete_flag": false,
        "manual_reg_flag": false,
        "out_flag": false,
        "out_datetime": null,
        "classification": 6,
        "others": "Guest"
      }
    }

在Alamofire中使用PUT请求的API服务

func updateParticipant(updateType: UpdateParticipantType,
                       participantID: String,
                       completionHandler: @escaping(([Attendee]?, Error?) -> Void)) {

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

    let headers: HTTPHeaders = [
        "Content-Type": "application/json"
    ]

    let parameters: Parameters = [
        "registered_flag": false,
        "registration_type": 0
    ]

    Alamofire.request(updateParticipantURL!, method: .put, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON { (response) in

        switch response.result {
        case .success:
            print("Done")

            if let jsonArray = response.result.value as? [[String : Any]] {
                for anItem in jsonArray {
                    if let eventparticipant = anItem["event_participants"] as? [[String : Any]] {
                        var extractedAttendee = [Attendee]()
                        for participant in eventparticipant{
                            print(participant)
                            let success = Attendee.init(JSON: participant)
                            extractedAttendee.append(success!)
                        }
                        completionHandler(extractedAttendee, nil)
                    }
                }
            }

        case .failure(let error):
            completionHandler(nil, error)
        }
    }
}

与会者结构

struct Attendee: Decodable {

let id: String
let employeeNumber: String?
let lastName: String
let firstName: String
let middleName: String
let displayName: String
let department: String
let position: String
let registeredFlag: Bool
let registeredDateTime: Date?
let registrationType: Int
let deleteFlag: Bool
let manualFlag: Bool
let outFlag: Bool
let outDateTime: Date?
let classification: Int?
let others: String?

postman response

0 个答案:

没有答案