Some Fields are not going to the server as json object in Alamofire

Viewed 44

I am trying to send JSON object with test card details to the server by the following format.

{
  "card": {
    "number": "5555555555554444",
    "name_on_card": "TEST",
    "security_code": 123,
    "expiry_month": 12,
    "expiry_year": 30
  },
  "payment_card_session_id": "SESSIONXXXXXXXXXXXXXX",
  "device_data": {
        "browser": "SAFARI",
        "browserDetails": {
            "3DSecureChallengeWindowSize": "FULL_SCREEN",
            "acceptHeaders": "application/json",
            "colorDepth": 24,
            "javaEnabled": true,
            "language": "en-US",
            "screenHeight": 640,
            "screenWidth": 480,
            "timeZone": 273
        },
        "ipAddress": "127.0.0.1"
    }
}

I sent like this.

     let param = [
            "card":[
                "number": cardNum,
                "name_on_card": cardName as Any,
                "security_code": securityCode as Any,
                "expiry_month": expiryMonth as Any,
                "expiry_year": expiryYear as Any
            ],
            "payment_card_session_id": self.sessionID ?? "",
            "device_data": [
                "browser": "SAFARI",
                "browserDetails": [
                    "3DSecureChallengeWindowSize": "FULL_SCREEN",
                    "acceptHeaders": "application/json",
                    "colorDepth": 24,
                    "javaEnabled": true,
                    "language": "en-US",
                    "screenHeight": 540,
                    "screenWidth": 375,
                    "timeZone": 273
                ],
                "ipAddress": "127.0.0.1"
            ]
        ] as [String : Any]
       
        print("Sent Params for Authenticate Payer: \(param)")
     //   param3d = ["payment_card_session_id": self.sessionID ?? ""] as [String : AnyObject]
        AF.request(url, method: .post, parameters: param, encoding: URLEncoding(), headers: headers).responseJSON { response in
            switch response.result {
            case let .success(value):
                let json = JSON(value)
                print("authenticate payer Responce: \(json)")
            case let .failure(error):
                print("Error: \(error)")
}

I got this error as the response

{ "card" : [ "This field is required." ], "device_data" : [ "This field is required." ] }

can anyone figure out what's wrong here and correct in the answer, please. Thanks in Advance.

1 Answers

It's working when I changed to JSONEncoding() from URLEncoding(). Thanks guys for the comments.

Related