NSURLRequest to NSString

前端 未结 5 1844
渐次进展
渐次进展 2021-02-19 01:03

how can i convert NSURLRequest to NSString ?

5条回答
  •  孤城傲影
    2021-02-19 01:43

    If you're using swift, you can implement the following extension:

    extension NSURLRequest: DebugPrintable {
        public func extendedDescription() -> String {
    
            var result = "<\(NSStringFromClass(self.dynamicType)): " + String(format: "%p", self)
            result += "; HTTPMethod=" + (HTTPMethod ?? "nil") + "; URL=\(URL); timeoutInterval=" + String(format: "%.1fs", timeoutInterval) + "> {"
    
            // Add header fields.
            if let headers = allHTTPHeaderFields {
                result += "\nallHTTPHeaderFields {"
                for (key, value) in headers {
                    result += "\n\t\(key) : '\(value)'"
                }
                result += "\n}"
            }
    
            if let body = HTTPBody {
                result += "\nHTTPBody {\n " + ((NSString(data: body, encoding: NSASCIIStringEncoding) ?? "") as String) + "}"
            }
    
            return result + "\n}"
        }
    
        public override var debugDescription: String {
            return extendedDescription()
        }
    
        public override var description: String {
            return extendedDescription()
        }
    }
    

    Or check out this gist: https://gist.github.com/tomaskraina/d644af49e4968de85e34

    If printed out, it displays something similar to the following:

     {
    allHTTPHeaderFields {
        Accept-Encoding : 'gzip'
        Content-Type : 'application/json'
        Accept : 'application/json'
    }
    HTTPBody {
        {"key1":"value1"}
    }
    }
    

提交回复
热议问题