2

How to convert URL Request to cURL? I know there are plugins available in Alamofire / Moya.

But I'm using native code.

Here is how I'm generating URLRequest.

var urlRequest = URLRequest(url: url) // URL is a param herel
var newHeaders: [String: String]
if let cookies = HTTPCookieStorage.shared.cookies {
  newHeaders = HTTPCookie.requestHeaderFields(with: cookies)
} else {
  newHeaders = [: ]
}
newHeaders = // add few more headers here
urlRequest.allHTTPHeaderFields = newHeaders
urlRequest.httpBody = data // set request body

How to log a cURL of above request?

Sagar R. Kothari
  • 23,587
  • 48
  • 158
  • 226
  • 1
    There is no built it option. So you need to do it yourself. What about https://github.com/Alamofire/Alamofire/blob/master/Source/Request.swift#L876 ? It's quite straightforward. " I know there are plugins available in Alamofire / Moya; But I'm using native code." What's blocking you from getting inspired with their code, how they do it, etc.? – Larme May 17 '20 at 18:27

1 Answers1

5

You can simply try this URLRequest to cURL

To use it in your code first import that extension and then simply call urlRequest.cURL() or urlRequest.cURL(pretty: true) to print pretty formatted curl.

Abhishek Maurya
  • 485
  • 3
  • 13