0

我在与 REST 服务器通信时遇到问题。我可以使用 Postman 作为测试以确保 JSON 正文正确,从而成功拨打电话,如下所示:

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

但是,使用 Swift,我首先生成了一个JSON_bodyObj,我用它来创建一个URLRequest,我在其中设置主体 via:try self.request.httpBody = JSONSerialization.data(withJSONObject: JSON_bodyObj)当我尝试请求时失败(从服务器返回错误)。

这是我构造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()

当我消除_autoBracket块时,请求成功(但显然,它并没有完成我需要它做的所有事情)。

我是否错误地构建了初始JSON_bodyObj

4

0 回答 0