Alamofire Post Request Issues

我怕爱的太早我们不能终老 提交于 2019-12-03 14:02:23

问题


   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 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."


回答1:


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.



来源:https://stackoverflow.com/questions/44277005/alamofire-post-request-issues

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!