File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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+ }
Original file line number Diff line number Diff line change @@ -19,3 +19,4 @@ extension URLResponse {
1919 return response
2020 }
2121}
22+
Original file line number Diff line number Diff 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 {
You can’t perform that action at this time.
0 commit comments