diff --git a/Changelog.md b/Changelog.md index ed5822902..e825360ce 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,4 +1,6 @@ # Next +### Fixed +- Fixed a bug with missing Content-Type header when using `.requestJSONEncodable` [#1410](https://github.com/Moya/Moya/pull/1410) by [@Vict0rS](https://github.com/Vict0rS). # [10.0.0] - 2017-10-21 ### Fixed diff --git a/Sources/Moya/URLRequest+Encoding.swift b/Sources/Moya/URLRequest+Encoding.swift index 74c84b78c..87302635b 100644 --- a/Sources/Moya/URLRequest+Encoding.swift +++ b/Sources/Moya/URLRequest+Encoding.swift @@ -6,6 +6,12 @@ internal extension URLRequest { do { let encodable = AnyEncodable(encodable) httpBody = try JSONEncoder().encode(encodable) + + let contentTypeHeaderName = "Content-Type" + if value(forHTTPHeaderField: contentTypeHeaderName) == nil { + setValue("application/json", forHTTPHeaderField: contentTypeHeaderName) + } + return self } catch { throw MoyaError.encodableMapping(error) diff --git a/Tests/EndpointSpec.swift b/Tests/EndpointSpec.swift index 1dd5ff50c..cfe579aec 100644 --- a/Tests/EndpointSpec.swift +++ b/Tests/EndpointSpec.swift @@ -162,9 +162,15 @@ final class EndpointSpec: QuickSpec { expect(issue.title).to(equal(expectedIssue.title)) } + it("updates headers to include Content-Type: application/json") { + let contentTypeHeaders = ["Content-Type": "application/json"] + let initialHeaderFields = endpoint.httpHeaderFields ?? [:] + let expectedHTTPHeaderFields = initialHeaderFields.merging(contentTypeHeaders) { initialValue, _ in initialValue } + expect(request.allHTTPHeaderFields).to(equal(expectedHTTPHeaderFields)) + } + it("doesn't update any of the other properties") { expect(request.url?.absoluteString).to(equal(endpoint.url)) - expect(request.allHTTPHeaderFields).to(equal(endpoint.httpHeaderFields)) expect(request.httpMethod).to(equal(endpoint.method.rawValue)) } }