Swift JSONSerialization, How to properly format for REST server?

71 Views Asked by At

I'm having trouble communicating with a REST server. I'm able to make the call with success using Postman as a test to make sure the JSON body is correct, which looks like:

{
    "name": "camera.setOptions",
    "parameters": {
        "options": {
            "exposureProgram": 2,
            "fileFormat": { 
                    "type": "raw+",
                    "width": 6720,
                    "height": 3360
            },
            "_autoBracket": {
                "_bracketNumber": 3,
                "_bracketParameters": [
                    {
                        "aperture": 2.1,
                        "iso": 400,
                        "shutterSpeed": 0.002
                    },
                    {
                        "aperture": 2.1,
                        "iso": 400,
                        "shutterSpeed": 0.008
                    },
                    {
                        "aperture": 2.1,
                        "iso": 400,
                        "shutterSpeed": 0.0333
                    }
                ]
            }
        }
    }
}

However, with Swift I first generate a JSON_bodyObj which I use to create a URLRequest, where I set the body via: try self.request.httpBody = JSONSerialization.data(withJSONObject: JSON_bodyObj) which fails (returns an error from the server) when I attempt the request.

Here is how I construct the JSON_bodyObj:

JSON_bodyObj = [
    "name": "camera.setOptions",
    "parameters": [
        "options": [
            "_function": "normal",
            "exposureProgram": 1,
            "fileFormat": [
                "type": "raw+",
                "width": 6720,
                "height": 3360
            ],
            "_autoBracket": [
                "_bracketNumber": 3,
                "_bracketParameters" : [
                    [
                        "aperture": 2.1,
                        "iso": 400,
                        "shutterSpeed": 0.002
                    ],
                    [
                        "aperture": 2.1,
                        "iso": 400,
                        "shutterSpeed": 0.008
                    ],
                    [
                        "aperture": 2.1,
                        "iso": 400,
                        "shutterSpeed": 0.0333
                    ]
                ]
            ]
        ]
    ]
]

self.request.setValue("application/json;charset=utf-8", forHTTPHeaderField: "Content-Type")
self.request.setValue("application/json", forHTTPHeaderField: "Accept")
self.request.httpMethod = "POST"

try self.request.httpBody = JSONSerialization.data(withJSONObject: JSON_bodyObj)
let task = URLSession.shared.dataTask(with: self.request) { (data, response, error)//Closure }
task.resume()

When I eliminate the _autoBracket block the Request succeeds (though, obviously, it does not do all I need it to do).

Am I constructing the initial JSON_bodyObj incorrectly?

0

There are 0 best solutions below