diff --git a/netfox/Core/NFXProtocol.swift b/netfox/Core/NFXProtocol.swift index 405805f4..2f3087a1 100755 --- a/netfox/Core/NFXProtocol.swift +++ b/netfox/Core/NFXProtocol.swift @@ -59,9 +59,43 @@ open class NFXProtocol: URLProtocol let mutableRequest = (request as NSURLRequest).mutableCopy() as! NSMutableURLRequest URLProtocol.setProperty(true, forKey: NFXProtocol.nfxInternalKey, in: mutableRequest) + + if let bodyData = captureRequestBody(mutableRequest as URLRequest) { + mutableRequest.httpBody = bodyData + } + session.dataTask(with: mutableRequest as URLRequest).resume() } + func captureRequestBody(_ request: URLRequest) -> Data? { + guard let _ = request.httpBody else { + return nil + } + + guard let bodyStream = request.httpBodyStream else { + return nil + } + + bodyStream.schedule(in: RunLoop.current, forMode: .default) + + let data = NSMutableData() + var buffer = [UInt8](repeating: 0, count: 1024) + bodyStream.open() + while bodyStream.hasBytesAvailable { + let length = bodyStream.read(&buffer, maxLength: 1024) + if length == 0 { + break + } else { + data.append(&buffer, length: length) + } + } + + bodyStream.remove(from: RunLoop.current, forMode: .default) + bodyStream.close() + + return data as Data + } + override open func stopLoading() { session.getTasksWithCompletionHandler { dataTasks, _, _ in