File tree Expand file tree Collapse file tree
SimpleHTTPFoundation/HTTP Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -22,8 +22,6 @@ public struct Request<Output> {
2222 public let method : Method
2323 public let body : Body ?
2424 public let query : [ String : QueryParam ]
25- /// content type to use for encoding the request body. If nil, the session default is used
26- public var contentType : HTTPContentType ?
2725 public var cachePolicy : URLRequest . CachePolicy = . useProtocolCachePolicy
2826 public var headers : HTTPHeaderFields = [ : ]
2927
Original file line number Diff line number Diff line change @@ -71,9 +71,19 @@ public class Session {
7171extension Session {
7272 private func dataPublisher< Output> ( for request: Request < Output > ) async throws -> Response < Output > {
7373 let modifiedRequest = try await config. interceptor. adaptRequest ( request)
74- let requestContentType = modifiedRequest. contentType ?? config. data. defaultType
74+ let requestContentType = modifiedRequest. headers. contentType ?? config. data. defaultType
75+ let encoder : ContentDataEncoder ?
76+
77+ // FIXME: we also check body inside toURLRequest
78+ switch modifiedRequest. body {
79+ case . encodable:
80+ encoder = config. data. encoder [ requestContentType]
81+ case . multipart, . none:
82+ // this one is supposed to never be nil
83+ encoder = config. data. encoder [ config. data. defaultType]
84+ }
7585
76- guard let encoder = config . data . encoder [ requestContentType ] else {
86+ guard let encoder else {
7787 throw SessionConfigurationError . missingEncoder ( requestContentType)
7888 }
7989
Original file line number Diff line number Diff line change @@ -31,3 +31,18 @@ extension HTTPHeader {
3131 public static let proxyAuthorization : Self = " Proxy-Authorization "
3232 public static let wwwAuthenticate : Self = " WWW-Authenticate "
3333}
34+
35+ public extension HTTPHeaderFields {
36+ /// - Returns the content type if HTTPHeader.contentType was set
37+ var contentType : HTTPContentType ? {
38+ self [ . contentType] . map ( HTTPContentType . init ( value: ) )
39+ }
40+
41+ func contentType( _ value: HTTPContentType ) -> Self {
42+ var copy = self
43+
44+ copy [ . contentType] = value. value
45+
46+ return copy
47+ }
48+ }
You can’t perform that action at this time.
0 commit comments