Alamofire发布请求问题 [英] Alamofire Post Request Issues

查看:274
本文介绍了Alamofire发布请求问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

   let parameters = [
        "checkout": [
            "email": "john.smith@example.com",
            "line_items": [
                "variant_id": 31342168513,
                "quantity": 1
                ],
            "shipping_address": [
                "first_name": "John",
                "last_name": "Smith",
                "address1": "126 York St.",
                "city": "Ottawa",
                "province_code": "ON",
                "country_code": "CA",
                "phone": "(123)456-7890",
                "zip": "K1N 5T5"
            ]
        ]
    ]

    let urlString = "https://\(Key):\(Password)@sapphireonline-staging.myshopify.com/admin/checkouts.json"
    let headers: HTTPHeaders = [
        "X-Shopify-Storefront-Access-Token": "5681ded39ead3fa6f4594ad0981367",
        "Content-Type": "application/json"
    ]

    Alamofire.request(urlString, method:.post, parameters: parameters,encoding: JSONEncoding.default, headers:headers).responseJSON { response in
        switch response.result {
        case .success:

            print("\n\n Alamofire Response . resposne :- ", response)
        case .failure(let error):

            print("\n\n Alamofire Failure :- ",error as NSError)
        }
    }

以下代码为什么给我错误:

Why does the following code give me the error:

Alamofire失败:-错误Domain = Alamofire.AFError代码= 4由于错误,JSON无法序列化:
由于数据不在其中,因此无法读取数据正确的格式。

Alamofire Failure :- Error Domain=Alamofire.AFError Code=4 "JSON could not be serialized because of error: The data couldn’t be read because it isn’t in the correct format."

推荐答案

服务器的响应不是JSON。也许您收到错误响应。建议您检查一下响应错误代码或使用curl尝试相同的请求。

The response from the server is not JSON. Maybe you got an error response. I suggest you check the response error code or try the same request using curl.

这篇关于Alamofire发布请求问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆