mirror of
https://github.com/kean/Pulse.git
synced 2026-05-30 21:07:33 +00:00
35 lines
1.1 KiB
Swift
35 lines
1.1 KiB
Swift
// The MIT License (MIT)
|
||
//
|
||
// Copyright (c) 2020–2022 Alexander Grebenyuk (github.com/kean).
|
||
|
||
import PulseCore
|
||
import Foundation
|
||
|
||
extension NetworkLoggerSummary {
|
||
func cURLDescription() -> String {
|
||
guard let request = currentRequest, let url = request.url, let method = request.httpMethod else {
|
||
return "$ curl command generation failed"
|
||
}
|
||
|
||
var components = ["curl -v"]
|
||
|
||
components.append("-X \(method)")
|
||
|
||
for header in request.headers {
|
||
let escapedValue = header.value.replacingOccurrences(of: "\"", with: "\\\"")
|
||
components.append("-H \"\(header.key): \(escapedValue)\"")
|
||
}
|
||
|
||
if let httpBodyData = requestBody {
|
||
let httpBody = String(decoding: httpBodyData, as: UTF8.self)
|
||
var escapedBody = httpBody.replacingOccurrences(of: "\\\"", with: "\\\\\"")
|
||
escapedBody = escapedBody.replacingOccurrences(of: "\"", with: "\\\"")
|
||
components.append("-d \"\(escapedBody)\"")
|
||
}
|
||
|
||
components.append("\"\(url.absoluteString)\"")
|
||
|
||
return components.joined(separator: " \\\n\t")
|
||
}
|
||
}
|