Last active
December 8, 2023 11:16
-
-
Save pchelnikov/405e3421fc280caa1f81eef6643dd71b to your computer and use it in GitHub Desktop.
URLRequest debugging
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Declare extension to URLRequest: | |
extension URLRequest { | |
public var curlString: String { | |
// Logging URL requests in whole may expose sensitive data, | |
// or open up possibility for getting access to your user data, | |
// so make sure to disable this feature for production builds! | |
#if !DEBUG | |
return "" | |
#else | |
var result = "curl -k " | |
if let method = httpMethod { | |
result += "-X \(method) \\\n" | |
} | |
if let headers = allHTTPHeaderFields { | |
for (header, value) in headers { | |
result += "-H \"\(header): \(value)\" \\\n" | |
} | |
} | |
if let body = httpBody, !body.isEmpty, let string = String(data: body, encoding: .utf8), !string.isEmpty { | |
result += "-d '\(string)' \\\n" | |
} | |
if let url = url { | |
result += url.absoluteString | |
} | |
return result | |
#endif | |
} | |
} | |
//Usage | |
//And log it: | |
//let request = ... | |
//print("\(request.curlString)") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment