Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 496
matteok Avatar asked Nov 29 '22 14:11

matteok


1 Answers

These methods will print the HTTP Request and Response in a clean way:

class func log(request: URLRequest){

    let urlString = request.url?.absoluteString ?? ""
    let components = NSURLComponents(string: urlString)

    let method = request.httpMethod != nil ? "\(request.httpMethod!)": ""
    let path = "\(components?.path ?? "")"
    let query = "\(components?.query ?? "")"
    let host = "\(components?.host ?? "")"

    var requestLog = "\n---------- OUT ---------->\n"
    requestLog += "\(urlString)"
    requestLog += "\n\n"
    requestLog += "\(method) \(path)?\(query) HTTP/1.1\n"
    requestLog += "Host: \(host)\n"
    for (key,value) in request.allHTTPHeaderFields ?? [:] {
        requestLog += "\(key): \(value)\n"
    }
    if let body = request.httpBody{
        let bodyString = NSString(data: body, encoding: String.Encoding.utf8.rawValue) ?? "Can't render body; not utf8 encoded";
        requestLog += "\n\(bodyString)\n"
    }

    requestLog += "\n------------------------->\n";
    print(requestLog)
}

class func log(data: Data?, response: HTTPURLResponse?, error: Error?){

    let urlString = response?.url?.absoluteString
    let components = NSURLComponents(string: urlString ?? "")

    let path = "\(components?.path ?? "")"
    let query = "\(components?.query ?? "")"

    var responseLog = "\n<---------- IN ----------\n"
    if let urlString = urlString {
        responseLog += "\(urlString)"
        responseLog += "\n\n"
    }

    if let statusCode =  response?.statusCode{
        responseLog += "HTTP \(statusCode) \(path)?\(query)\n"
    }
    if let host = components?.host{
        responseLog += "Host: \(host)\n"
    }
    for (key,value) in response?.allHeaderFields ?? [:] {
        responseLog += "\(key): \(value)\n"
    }
    if let body = data{
        let bodyString = NSString(data: body, encoding: String.Encoding.utf8.rawValue) ?? "Can't render body; not utf8 encoded";
        responseLog += "\n\(bodyString)\n"
    }
    if let error = error{
        responseLog += "\nError: \(error.localizedDescription)\n"
    }

    responseLog += "<------------------------\n";
    print(responseLog)
}

Example:

---------- OUT ---------->
https://api.example.com/api/auth

POST /api/auth HTTP/1.1
Host: api.example.com
------------------------->


<---------- IN ----------
https://api.example.com/api/auth

HTTP/1.1 200 /api/auth
Host: api.example.com
Content-Type: application/json; charset=UTF-8
Content-Length: 331
Date: Mon, 21 Aug 2017 18:55:46 GMT
Server: Google Frontend

{
  "Data": {
    "Token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MDMzNDUzNDYsImlhdCI6MTUwMzM0MTc0NiwianRpIjoiNTcwNmMyN2UtODZhMi0xMWU3LThkN2ItNjJjYmY2YzkxYzdhIiwibmJmIjoxNTAzMzQxNzQ2fQ.0p09QG9ImjemQxlDIxZb9SL6j3Fy4VAAzsA-JZp27q0",
    "ExpiryUnix": 1503345346,
    "ExpiryTimestamp": "Mon Aug 21 19:55:46 UTC 2017"
  }
}
<------------------------
like image 194
W.K.S Avatar answered Dec 12 '22 04:12

W.K.S