0

When i try from Alamofire then it work fine but when i try to solve from URLSESSION Swift 4 then i got wrong response. I checked in postman and it's response was right.

Parameter Description: I have a key "data" whose value is another dictionary ["answer1":"1","answer2":"2","answer3":"3"]. Need to post this.

Wrong Reposnse = {"message = "Invalid data."; response = failure;}"

Right Reposnse = {"response":"success","message":"Data Inserted”}.

func postData()
{
    let BASEURLS = "http://sebamedretail.techizer.in/babystore_api/question_data"
    let parameter = ["data":["answer1":"1","answer2":"2","answer3":"3"]]
    let session = URLSession.shared
    var request = URLRequest.init(url: URL.init(string: BASEURLS)!)
    request.httpMethod = "POST"
    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    do
    {
        request.httpBody = try JSONSerialization.data(withJSONObject:parameter, options: [])
        let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
            do{
                let responseArr = try! JSONSerialization.jsonObject(with: data!, options: [])
            }
        })
        task.resume()
    }
    catch
    {}
}
dalton_c
  • 6,876
  • 1
  • 33
  • 42

1 Answers1

0

Everything seems fine, maybe you should check the way you extract the JSON in your code, if it's PHP, here could be a solution: https://stackoverflow.com/a/18867369/7452015

Angel
  • 402
  • 3
  • 8