File tree Expand file tree Collapse file tree 1 file changed +11
-8
lines changed
Sources/SwiftBoost/Foundation/Extensions Expand file tree Collapse file tree 1 file changed +11
-8
lines changed Original file line number Diff line number Diff line change @@ -66,17 +66,12 @@ public extension URLSession {
6666
6767 var request = URLRequest ( url: url)
6868 request. httpMethod = method. id
69-
70- // Content Type
71- if let contentTypeHeader {
72- request. setValue ( contentTypeHeader. id, forHTTPHeaderField: " Content-Type " )
73- }
74-
69+
7570 // Body
7671 if var body = body {
77-
72+
7873 body = body. compactMapValues { $0 } // delete `nil` values
79-
74+
8075 do {
8176 let jsonData = try JSONSerialization . data ( withJSONObject: body, options: [ ] )
8277 request. httpBody = jsonData
@@ -85,6 +80,14 @@ public extension URLSession {
8580 return
8681 }
8782 }
83+
84+ /* Default to application/json when a body is present. Without Content-Type,
85+ servers may fall back to form-urlencoded parsing, which treats `%20` and `+`
86+ as equivalent for spaces and can round-trip values back as `+`. */
87+ let resolvedContentType = contentTypeHeader ?? ( request. httpBody != nil ? . application_json : nil )
88+ if let resolvedContentType {
89+ request. setValue ( resolvedContentType. id, forHTTPHeaderField: " Content-Type " )
90+ }
8891
8992 // Make Request
9093 URLSession . shared. dataTask ( with: request) { ( data, response, error) in
You can’t perform that action at this time.
0 commit comments