swift - Api 调用无法在 alamofire 中工作,但在 postman 中工作

标签 swift alamofire postman

enter image description here我有一个 API 调用正在使用 postman 工作,但是当在 alamofire 中给出相同的数据时,它不起作用。请帮我。我花了大约 1 天的时间修复它。

        let parameters = [
        "paymenttype":"debitcard",
        "coupon":"",
        "products":[["pid":"72","qnty":"1"],["pid":"4","qnty":"1"],["pid":"3","qnty":"1"]],
        "grandtotal":"71499.52",
        "discount":"",
        "itemtotalval":"69417.0",
        "cgst":"1041.26",
        "comment":"sss",
        "sgst":"1041.26",
        "billingaddress":[["bfname":"debdeep nandy","bmobilenumber":"9875463215","bpincode":"823691" , "baddress":"kolkata","bcity":"kolkata","bstate":"West Bengal","bcountry":"india","bemail":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7612130036111b171f1a5815191b" rel="noreferrer noopener nofollow">[email protected]</a>"]],
        "shippingaddress":[["sfname":"debdeep nandy","smobilenumber":"9875463215","spincode":"823691","saddress":"kolkata","scity":"kolkata","sstate":"West Bengal","scountry":"india","semail":"india"]],
        "user_id":1
        ] as [String : AnyObject]
    Alamofire.request(
        URL(string:"http://jarsservices.com/pccj-app/apiv1/place_order")!,
        method: .post,
        parameters: parameters)
        .validate()
        .responseString { (response) -> Void in
           
            guard response.result.isSuccess else {
                print("Error while sending data: \(response.result.error)")
                return
            }

            if let value = response.result.value as? [String: Any] {
                DispatchQueue.main.async {
                   // self.activityView.stopAnimating()

                    if value["status"] as! Int == 1 {
                        self.view.makeToast("Thank you! Your feedback is recorded in server.")
                    }
                    else {
                        self.view.makeToast(value["msg"] as? String)
                    }
                }

            }
            else {
                print("Malformed data received from fetchAllRooms service")
                return
            }


    }

header 中没有身份验证。请帮帮我。

我已经添加了来自 postman 的正在运行的图像。

最佳答案

试试这个代码,

添加此扩展

extension Array where Element: Codable {

    public var toData: Data {
        let encoder = JSONEncoder()
        do {
            return try encoder.encode(self)
        }
        catch {
            fatalError(error.localizedDescription)
        }
    }

    public var toJson: String? {
        return toData.toJson
    }
}

extension Data {
    //  Convert NSData to String
    public var toJson : String? {
        return String(data: self, encoding: String.Encoding.utf8)
    }
}

您的 Web API 参数 如下

let parameters: [String : Any] = [
        "paymenttype":"debitcard",
        "coupon":"",
        "products":[["pid":"72","qnty":"1"],["pid":"4","qnty":"1"],["pid":"3","qnty":"1"]].toJson ?? "[]",
        "grandtotal":"71499.52",
        "discount":"",
        "itemtotalval":"69417.0",
        "cgst":"1041.26",
        "comment":"sss",
        "sgst":"1041.26",
        "billingaddress":[["bfname":"debdeep nandy","bmobilenumber":"9875463215","bpincode":"823691" , "baddress":"kolkata","bcity":"kolkata","bstate":"West Bengal","bcountry":"india","bemail":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0763627147606a666e6b2964686a" rel="noreferrer noopener nofollow">[email protected]</a>"]].toJson ?? "[]",
        "shippingaddress":[["sfname":"debdeep nandy","smobilenumber":"9875463215","spincode":"823691","saddress":"kolkata","scity":"kolkata","sstate":"West Bengal","scountry":"india","semail":"india"]].toJson ?? "[]",
        "user_id":1
        ]

你的其他部分是

else {
    print("Malformed data received from fetchAllRooms service")
    debugPrint(String(data: response.data!, encoding: String.Encoding.utf8)) 
    //Print out our data 
    return
}

enter image description here

注意:Alamofire参数中使用JSON字符串格式传递ArrayDictionary值,然后就可以了。

关于swift - Api 调用无法在 alamofire 中工作,但在 postman 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50062456/

相关文章:

验证失败时 Alamofire 解析响应数据

json - 使用 Alamofire API Rest 发送空数组

ios - Alamofire - 对成员 'upload(_:to:method:headers:)' 的模糊引用

graphql - 如何通过 postman 发送graphql查询?

ios - SPTYourMusic 请求后,包含 SPTListPage 的响应中的项目为零

swift - 类型为 'representedObject' 的属性 'AnyObject?' 无法覆盖类型为 'Any?' 的属性

ios - 在选项卡栏上 View Controller 的内容之前显示登录屏幕

swift - 当第二个 View Controller 启动时停止第一个 View Controller 的动画快速编程

java - 如何使用 postman 休息客户端发送对象来调用REST服务

api - 为什么 api 响应正文未显示在 postman 应用程序生成的文档中?