Loading repository data…
Loading repository data…
akashkottil / repository
Network Logger / cURL Logger for iOS / Swift / Alamofire This is a lightweight Swift library / utility to help you log HTTP requests (and optionally responses) in cURL format during development.
A transparent discovery signal based on current public GitHub metadata.
This score does not audit code, security, maintainers, documentation quality, or suitability. Verify the repository and its current documentation before adoption.
A simple Swift utility to print HTTP requests as cURL commands (and optionally responses) for debugging.
Works with both Alamofire and URLSession.
Just include the NetworkLogger.swift (and its extensions) into your project, for example in a Utilities or Networking folder.
No external dependency beyond Foundation (and Alamofire if you use it).
Open NetworkDebugConfig and set:
struct NetworkDebugConfig {
/// Enable or disable all logging
static var isEnabled: Bool = true
/// If `true`, response bodies (& status, headers) also get printed
static var logResponses: Bool = false
}
In development, use isEnabled = true. Before releasing, set it to false to silence all logging.
After you make an Alamofire request, append .logCurl() before validation / response handlers:
AF.request(
url,
method: .post,
parameters: requestModel,
encoder: JSONParameterEncoder.default,
headers: headers
)
.logCurl()
.validate()
.responseDecodable(of: SomeResponse.self) { response in
// … your handling …
}
This will print something like:
================================================================================
🌐 cURL REQUEST
================================================================================
curl -X POST \
"https://api.example.com/path" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer …" \
-d "{\"key\":\"value\"}"
================================================================================
For URLRequest–based calls (async/await, delegates, etc.), just call:
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
// … set other headers, body, etc.
request.logCurl()
let (data, response) = try await URLSession.shared.data(for: request)
This logs the same style cURL command before sending.
If NetworkDebugConfig.logResponses is true, responses will be printed (status, headers, body).
generateCurlCommand(from:): builds up an array of command parts — HTTP method, URL, headers, and body — escapes quotes/newlines, and then joins them into a nicely line-broken cURL command.guard NetworkDebugConfig.isEnabled checks in each logging function ensure nothing is printed when logging is disabled.NetworkLogger.logResponse(data:response:error:) — you may call that manually (e.g. in URLSession.data completion) or integrate into your networking wrappers.DataRequest.logCurl() – logs an Alamofire requestURLRequest.logCurl() – logs a raw request// Alamofire example
AF.request("https://api.myserver.com/login",
method: .post,
parameters: ["user": "john", "password": "secret"],
encoder: JSONParameterEncoder.default,
headers: nil)
.logCurl()
.responseDecodable(of: LoginResponse.self) { resp in
print("Status:", resp.response?.statusCode ?? -1)
}
// URLSession example
var req = URLRequest(url: URL(string: "https://api.myserver.com/data")!)
req.httpMethod = "GET"
req.setValue("Bearer token...", forHTTPHeaderField: "Authorization")
req.logCurl()
let (data, res) = try await URLSession.shared.data(for: req)
print("Received \(data.count) bytes, status:", (res as? HTTPURLResponse)?.statusCode ?? -1)
isEnabled = true in production builds, as sensitive data (tokens, payloads) may be exposed in logs.print(...). In extremely high-volume networking (many requests per millisecond), logging may add overhead.NetworkLogger to forward to that instead of plain print.Contributions are welcome! Some possible enhancements:
logResponse(...) wherever appropriate (e.g. wrap Alamofire's internal response pipeline)Authorization)Data attachments)This project is open source. Use it freely in your apps. (You may include a license file — e.g. MIT — in the repo.)
This “Network‑Logger” utility gives you a quick and easy way to see exactly what HTTP requests your app is making, in a reproducible cURL format. It helps reduce friction when debugging APIs, replicating HTTP traffic, or diagnosing server interactions. It’s simple, unobtrusive, and easy to disable when you move into production.
Enjoy debugging! 🚀