2016-01-10 13 views
8

È possibile stampare l'intera richiesta http prima di effettuare la richiesta effettiva?come stampare la richiesta http sulla console

Questo è il mio codice:

let postsEndpoint: String = "https://www.example.com/api/" 
guard let postsURL = NSURL(string: postsEndpoint) else { 
    throw APICallError.other("cannot create URL") 
} 
let postsURLRequest = NSMutableURLRequest(URL: postsURL) 
postsURLRequest.HTTPMethod = "POST" 
print(UTF8EncodedJSON) 
postsURLRequest.HTTPBody = UTF8EncodedJSON 
print(postsURLRequest) 

let config = NSURLSessionConfiguration.defaultSessionConfiguration() 
let session = NSURLSession(configuration: config) 

let task = session.dataTaskWithRequest(postsURLRequest, completionHandler: { 
    (data, response, error) in 
    //handle response 
}) 

questo stampa il JSON in esadecimale e poi:

<NSMutableURLRequest: 0x7fdae8d1dd30> { URL: https://www.ritzie.nl/api/v2 }

che non mi aiuta molto. Voglio solo avere la mia intera richiesta stampata come la vedresti in firebug su firefox, per esempio.

--edit--

Per chiarire, io non sto cercando di stampare il mio JSON. Ci sono già abbastanza domande a riguardo su SO. Voglio che la mia richiesta completa stampato qualcosa del tipo:

POST /api/v2/ HTTP/1.1 

HTTP headers: 
Host: www.example.ocm 
Origin: http://www.example.com 
Connection: keep-alive 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/\*;q=0.8 
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/601.3.9 (KHTML, like Gecko) Version/9.0.2 Safari/601.3.9 
Referer: http://www.ritzie.nl/api/test.php 
Accept-Language: en-us 
Accept-Encoding: gzip, deflate 

request body: 
data=%7B%22action%22%3A+%22vehicleRecords%22%2C%0D%0A%22token%22%3A+%22token_04e01fdc78205f0f6542bd523519e12fd3329ba9%22%2C%0D%0A%22vehicle%22%3A+%22vehicle_e5b79b2e%22%7D 

o questo:

Firebug request screenshot

+0

Se volete vedere il testo JSON è necessario creare una stringa dei dati è tutto – Wain

+0

@Wain, lol no, voglio vedere la mia intera richiesta http. Come questo: 'GET/api/v2/HTTP/1.1 intestazioni HTTP: ospitanti: www.ritzie.nl Accept: */* Cookie: PHPSESSID = vd61qutdll216hbs3a677fgsq4 User-Agent: KM% 20registratie% 20tabbed% 20NL/1 CFNetwork/758.2.8 Darwin/15.2.0 Accept-Language: it-it Accept-Encoding: gzip, deflate Connessione: keep-alive' – Fr4nc3sc0NL

+0

Quindi è necessario stampare le intestazioni da soli – Wain

risposta

0

Stampa all'interno del completionHandler:

let task = session.dataTaskWithRequest(postsURLRequest, completionHandler: { 
    (data, response, error) in 
    //handle response 
    print(NSString(data: data.HTTPBody!, encoding:NSUTF8StringEncoding)!) 
}) 

se non ha funzionato, allora utilizzare il seguente:

print(NSString(data: data, encoding: NSUTF8StringEncoding)) 
+0

stai stampando il corpo della risposta, voglio l'intera richiesta – Fr4nc3sc0NL

1

Questo funziona per me:

let task = session.dataTaskWithRequest(postsURLRequest, completionHandler: { 
     (data, response, error) in 
     if let _data = data 
     { 
      do { 
       var jsonResult: NSDictionary 
       try jsonResult = NSJSONSerialization.JSONObjectWithData(_data, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary 
       print("AsSynchronous\(jsonResult)") 
      } 
      catch { 
       // handle error 
      } 
     } 
     else 
     { 
      print("Error: no data for request \(urlPath)") 
     } 
    }) 
-1
let task = session.dataTaskWithRequest(postsURLRequest, completionHandler: { 
    (data, response, error) in 
    //handle response 
    print(NSString(data: response.request, encoding:NSUTF8StringEncoding)!) 
}) 
+0

Non c'è alcuna proprietà richiesta sull'oggetto risposta. – Nate

Problemi correlati