andysmart / Timberjack

Automatic network activity logger for iOS and OSX
MIT License
159 stars 43 forks source link

Log POST params (body) #7

Open linakis opened 8 years ago

linakis commented 8 years ago

Is there any way to include the post body in the logged data?

shawnwall commented 8 years ago

+1

andysmart commented 8 years ago

Sadly, we're a little limited due to this bug (http://openradar.appspot.com/15993891), but if anyone has ideas for a workaround, I'm all ears!

I'll be revisiting Timberjack for Swift 3, so I'll do a little digging to see if anything has changed

zgthompson commented 8 years ago

I've been able to log the post body by reading the HTTPBodyStream property of an NSURLRequest. If you're interested, I can send a PR your way.

private extension NSURLRequest {

    func body() -> NSData? {
        var data: NSData?

        if let body = self.HTTPBody {
            data = body
        } else if let stream = self.HTTPBodyStream {
            stream.open()
            data = stream.readData()
            stream.close()
        }

        return data
    }
}

private extension NSInputStream {

    func readData() -> NSData {
        let maxLength = 4096

        let data = NSMutableData()

        var buffer = Array<UInt8>(count:maxLength, repeatedValue: 0)

        var bytesRead = self.read(&buffer, maxLength: maxLength)
        while bytesRead > 0 {
            data.appendBytes(&buffer, length: bytesRead)
            bytesRead = self.read(&buffer, maxLength: maxLength)
        }

        return data
    }
}