iosswiftalamofirexcode7.1

Alamofire HTTP requests fails


I made some HTTP requests using Alamofire. Some request has been succeeded and some are failed.

The error is:

Invalid value around character 0.

Failed request gave me above error.

Below I have mentioned a sample code which failed:

let parameters = ["amount": ["10"], "payment_method": ["paypal"], "date": ["2015-11-25"], "details": ["Payment description"]]
    
    let headers = [
        "Accept": "*/*",
        "Content-Type": "application/json"
    ]
    
    let url = "https://livetest.somedomain.com/api/invs/LAT1j5da99PdPg/payments?auth_token=pbtTEPNki3hUhGBuPX3d"
    
    Alamofire.request(.POST, url, parameters: parameters, encoding: .JSON, headers: headers)
        .responseJSON { response in
            let results = response.result
            print(results)
            print(response.debugDescription)
    }

Please help me to find the issue.


Solution

  • This issue was happened because of wrong format of JSON passing. Then i changed the parameter as follows

    let parameters = ["payment":["amount": "100" , "payment_method": "check", "date": "2015-11-25", "details": "Payment description dimuth Lasantha"]]
    

    Now it passes the correct format which is

    {
    payment: {
        "amount" : "100",
        "payment_method" : "check",
         .....
        }
    }