在 Swift 中使用 JSON 正文发出 HTTP Post 请求

Posted

技术标签:

【中文标题】在 Swift 中使用 JSON 正文发出 HTTP Post 请求【英文标题】:making HTTP Post request with JSON body in Swift 【发布时间】:2017-02-26 11:57:01 【问题描述】:

我是 swift 的新手,并试图发出一个调用 api 的 http post 请求。while it works just fine on postman

这是我需要将后端作为 httpBody 的 JSON 字符串。

myJSON ="address1":"Mirpur","address2":"D6, f8","cellPhone":"01852540565","city":"fghff","countryName":"Bangladesh","orderDate":"2017-02-25T11:28:24","orderStatus":1,"orderedProductList":["discount":0.0,"orderDetailId":0,"price":30000.0,"quantity":1,"shippingCharge":50.0,"supplierId":0,"tax":0.0,"type":"isBook":false,"typeId":0,"productId":5,"productViews":0,"discount":0.0,"orderDetailId":0,"price":50000.0,"quantity":1,"shippingCharge":50.0,"supplierId":0,"tax":0.0,"type":"isBook":false,"typeId":0,"productId":8,"productViews":0,"discount":0.0,"orderDetailId":0,"price":2000.0,"quantity":1,"shippingCharge":50.0,"supplierId":0,"tax":0.0,"type":"isBook":false,"typeId":0,"productId":9,"productViews":0],"paymentTransactionId":"1215455638874521","state":"fyy","zip":"4525","countryId":23,"orderId":0,"orderTotal":82000.0,"paymentMethodId":1,"userId":0

知道如何解决这个问题,这将非常有帮助。 谢谢

【问题讨论】:

字典语法在swift中使用[],而不是 同样在这种情况下,您需要指定变量的类型:let jsonObject: [String:Any] = ... 至于发出POST请求,试试Alamofire 我同意 deadbeef,Alamofire 强烈推荐用于 Swift 网络。 【参考方案1】:

使用 @deadbeef 建议的语法应该可以工作,例如重新格式化的 myJSON:

    let myJSON: [String : Any] = ["address1":"Mirpur",
                                "address2":"D6, f8",
                                "cellPhone":"01852540565",
                                "city":"fghff",
                                "countryName":"Bangladesh",
                                "orderDate":"2017-02-25T11:28:24",
                                "orderStatus":1,
                                "orderedProductList":[
                                    ["discount":0.0,
                                     "orderDetailId":0,
                                     "price":30000.0,
                                     "quantity":1,
                                     "shippingCharge":50.0,
                                     "supplierId":0,
                                     "tax":0.0,
                                     "type":[
                                        "isBook":false,
                                        "typeId":0
                                        ],
                                     "productId":5,"productViews":0
                                    ],
                                    ["discount":0.0,
                                     "orderDetailId":0,
                                     "price":50000.0,
                                     "quantity":1,
                                     "shippingCharge":50.0,
                                     "supplierId":0,"tax":0.0,
                                     "type":[
                                        "isBook":false,
                                        "typeId":0
                                        ],
                                     "productId":8,
                                     "productViews":0
                                    ],
                                    ["discount":0.0,
                                     "orderDetailId":0,
                                     "price":2000.0,
                                     "quantity":1,
                                     "shippingCharge":50.0,
                                     "supplierId":0,"tax":0.0,
                                     "type":[
                                        "isBook":false,
                                        "typeId":0
                                        ],
                                     "productId":9,
                                     "productViews":0
                                    ]
                                ],
                                "paymentTransactionId":"1215455638874521",
                                "state":"fyy",
                                "zip":"4525",
                                "countryId":23,
                                "orderId":0,
                                "orderTotal":82000.0,
                                "paymentMethodId":1,
                                "userId":0]

【讨论】:

以上是关于在 Swift 中使用 JSON 正文发出 HTTP Post 请求的主要内容,如果未能解决你的问题,请参考以下文章

如何调试网络请求 Swift

斜角 | CustomPOST:我无法发出 CORS POST 请求,正文中有 json 数据对象

Alamofire 在 swift 中使用正文发布请求

如何在使用 Cloud Scheduler 发出的 HTTP POST 请求上添加 JSON 正文?它会添加“Content-Type”:“application/json”标头吗?

如何在 Swift 中使用 Alamofire 将参数作为正文发送到 PUT 方法

Swift:带有 json 正文的 alamofire 帖子