Search code examples
iosswiftloggingswift2nsurlsession

log NSURLSession requests in console


Is it possible to log requests sent by NSURLSession to the console? I'm having issues with authentication and I can't debug if I can't look at the request


Solution

  • A possible way to print the body of a request is:

    let sBody = NSString(data: request.HTTPBody!, encoding: NSASCIIStringEncoding)
    print(sBody)
    

    To print all the headers:

    let sHeaderFields = request.allHTTPHeaderFields
    print(sHeaderFields)