2

I got response from api via postman

{
"status": "1",
"error": false,
"message": "Your order has been placed successfully"
}

I called api and pass the params which required to call api. The code is giving an error "Invalid value around character 1."

    let urlsContainer = UrlsContainer()
    let url = URL(string: urlsContainer.allotRunnerAPI)
    let session = URLSession.shared
    var request = URLRequest(url: url!)
    let postString = "user_id=\(user_id)&pincode=\(pincode)&select_address=\(select_address)&store_id=\(store_id)"
    request.httpMethod = "POST"
    request.httpBody = postString.data(using: .utf8)
    request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
    let task = session.dataTask(with: request as URLRequest, completionHandler: { data, response, error in
    guard error == nil else {
    return
    }
    guard let data = data else {
    return
    }

The code should execute the do block of code

do {
    let parsedData = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as! [String: AnyObject]
    print(parsedData)
    }

but now because of some problem in code it is executing catch block of code

 catch let error {
    print(error)
    }
   })
  task.resume()

I am not able to find the problem in my code to resolve the error

Rocky
  • 2,903
  • 1
  • 22
  • 26
Sanjay Mishra
  • 672
  • 7
  • 17

1 Answers1

0

use [String:String] to send data to api

let params = ["user_id":user_id,
"pincode":pincode,"select_address":select_address,
"store_id":store_id]
let data = JSONSerialization.data(withJSONObject: params, options: .prettyPrinted)

post this data as your body

Shahzaib Qureshi
  • 989
  • 8
  • 13
  • 3
    Never *prettyPrint* JSON which is being sent to a server. The server doesn't care for legibility at all. Omit the `options` parameter – vadian Feb 20 '19 at 09:49