Skip to content

Commit 7514d9d

Browse files
committed
Adds form URL encoding and configures logging
Adds functionality to encode parameters to `x-www-form-urlencoded` format. It allows to set the content type and request body. Configures the ability to disable logging of request and response bodies. Improves logging with conditional body output for better readability and control.
1 parent 0ce96c8 commit 7514d9d

3 files changed

Lines changed: 23 additions & 3 deletions

File tree

Sources/URLRequest+.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,18 @@ private extension String {
151151
self.utf8.allSatisfy { $0 & 0x80 == 0 }
152152
}
153153
}
154+
155+
extension URLRequest {
156+
mutating func setXWWWFormUrlencoded(_ parameters: [String: String]) {
157+
let bodyString = parameters.map { "\($0.key)=\($0.value)" }
158+
.joined(separator: "&")
159+
160+
// Convert string to Data
161+
guard let bodyData = bodyString.data(using: .utf8) else {
162+
fatalError("Failed to encode body data")
163+
}
164+
165+
self.httpBody = bodyData
166+
self.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
167+
}
168+
}

Sources/URLResponse+.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ extension URLResponse {
1919
return response
2020
}
2121
}
22+

Sources/URLSession+.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ public extension URLSession {
1616
struct Config {
1717
public var taskDelegate: URLSessionTaskDelegate? = defaultTaskDelegate
1818
public var logger: Logger? = defaultLogger
19+
public var logBody: Bool = logBody
1920

2021
public static var defaultTaskDelegate: URLSessionTaskDelegate?
2122
public static var defaultLogger = Logger.networking
23+
public static var logBody: Bool = true
2224

2325
public init() {}
2426
}
@@ -38,8 +40,9 @@ public extension URLSession {
3840
var config = config
3941
configurate?(&config)
4042

41-
config.logger?.debug("🛫 \(request.urlString)\n\(request.bodyString)\n📄 \(file.lastPathComponent)")
42-
43+
let reqBodyLog = config.logBody ? "\n\(request.bodyString)" : ""
44+
config.logger?.debug("🛫 \(request.urlString)\(reqBodyLog)\n📄 \(file.lastPathComponent)")
45+
4346
do {
4447

4548
let (data, urlResponse) = try await data(for: request, delegate: config.taskDelegate)
@@ -51,7 +54,8 @@ public extension URLSession {
5154
response: respones,
5255
data: data)
5356

54-
config.logger?.debug("🛬 \(dataResponse.request.urlString) \(dataResponse.status)\n\(dataResponse.bodyString)\n📄 \(file.lastPathComponent)")
57+
let respBodyLog = config.logBody ? "\n\(dataResponse.bodyString)" : ""
58+
config.logger?.debug("🛬 \(dataResponse.request.urlString) \(dataResponse.status)\n\(respBodyLog)\n📄 \(file.lastPathComponent)")
5559

5660
return dataResponse
5761
} catch {

0 commit comments

Comments
 (0)