Avinash Mishra Avinash Mishra - 3 months ago 10
Swift Question

Inside dataTaskWithRequest is not Executing. In Swift While Hitting the API

Same Data when I am hitting with POSTMAN I m getting the desired response. But in my Function inside dataTaskWithRequest I am Not Getting Any Response.

func sendData (){
let url = NSURL(string: "http://www.example.com/quiz-school/mobileData/request.php?request=QuizTotal&module=PQ")!
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.HTTPBody = try! NSJSONSerialization.dataWithJSONObject(dict, options: NSJSONWritingOptions())
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let task = NSURLSession.sharedSession().dataTaskWithRequest(request){ data, response, error in
if(error != nil){
print(error)
return
}
((response as! NSHTTPURLResponse).statusCode)
}
task.resume()
}


On DATA TASK

My Code Break Point
After Data Task

After DataTaskWithRequest
POSTMAN RESPONSE

Thanks Any Help Will Be appreciated.

My Dictionary Which I am Sending

{
data = (
{
answer = (
9353091
);
question = 31675931;
},
{
answer = (
9353101
);
question = 31675936;
},
{
answer = (
9353111
);
question = 31675941;
},
{
answer = (
9353121
);
question = 31675946;
},
{
answer = (
9353131
);
question = 31675951;
},
{
answer = (
9353141
);
question = 31675954;
},
{
answer = (
9353151
);
question = 31675961;
},
{
answer = (
9353279
);
question = 31676023;
},
{
answer = (
9353289
);
question = 31676026;
},
{
answer = (
9353299
);
question = 31676031;
}
);
end = 5565665;
quizId = 1206500;
start = 5565656;
}

Answer

Posting code working on my side based on our discussion:

//MARK: URLRequest

func sendData (){

    var dict = [String:AnyObject]()

    dict =
    [
        "data" : [
                [
                    "answer" : [9353091],
                    "question" : 31675931
                ],
                [
                    "answer" : [9353101],
                    "question"  : 31675936
                ]
        ],
        "end" : 5565665,
        "quizId" : 1206500,
        "start" : 5565656
    ]

    print(dict)

    let url = NSURL(string: "http://www.proprofs.com/quiz-school/mobileData/request.php?request=QuizTotal&module=PQ")!
    let request = NSMutableURLRequest(URL: url)
    request.HTTPMethod = "POST"
    request.HTTPBody = try!  NSJSONSerialization.dataWithJSONObject(dict, options: NSJSONWritingOptions())
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    let task =  NSURLSession.sharedSession().dataTaskWithRequest(request){ data, response, error in
        if(error != nil){
            print("ERRORRRRRRR : \(error)")
            return
        }

        print("RESPONSEEEEE : \(response)")

        do {
            let dic = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [String:AnyObject]

            print("DIIIIIIIC \(dic)")
        }catch {
            print("adsfdasfdsfdsafdsfadsfs")
        }

        ((response as! NSHTTPURLResponse).statusCode)
    }
    task.resume()
}