From fc789fcfc88f2ea9bde9d069101c76ed898ce712 Mon Sep 17 00:00:00 2001 From: Victor Sharavara Date: Wed, 25 Oct 2017 22:16:06 +0300 Subject: [PATCH 1/4] Added missing content type for requestJSONEncodable task --- Sources/Moya/URLRequest+Encoding.swift | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Sources/Moya/URLRequest+Encoding.swift b/Sources/Moya/URLRequest+Encoding.swift index 74c84b78c..de9c45a9e 100644 --- a/Sources/Moya/URLRequest+Encoding.swift +++ b/Sources/Moya/URLRequest+Encoding.swift @@ -6,6 +6,11 @@ internal extension URLRequest { do { let encodable = AnyEncodable(encodable) httpBody = try JSONEncoder().encode(encodable) + + if value(forHTTPHeaderField: "Content-Type") == nil { + setValue("application/json", forHTTPHeaderField: "Content-Type") + } + return self } catch { throw MoyaError.encodableMapping(error) From 2aa1d6bf7e47840b1c272691b170da4dd9829ae5 Mon Sep 17 00:00:00 2001 From: Victor Sharavara Date: Wed, 25 Oct 2017 23:10:35 +0300 Subject: [PATCH 2/4] Removed duplicating header name --- Sources/Moya/URLRequest+Encoding.swift | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Sources/Moya/URLRequest+Encoding.swift b/Sources/Moya/URLRequest+Encoding.swift index de9c45a9e..87302635b 100644 --- a/Sources/Moya/URLRequest+Encoding.swift +++ b/Sources/Moya/URLRequest+Encoding.swift @@ -7,8 +7,9 @@ internal extension URLRequest { let encodable = AnyEncodable(encodable) httpBody = try JSONEncoder().encode(encodable) - if value(forHTTPHeaderField: "Content-Type") == nil { - setValue("application/json", forHTTPHeaderField: "Content-Type") + let contentTypeHeaderName = "Content-Type" + if value(forHTTPHeaderField: contentTypeHeaderName) == nil { + setValue("application/json", forHTTPHeaderField: contentTypeHeaderName) } return self From 0a363c077a942d2ee6f6ba05e99772ebc3d55798 Mon Sep 17 00:00:00 2001 From: Victor Sharavara Date: Wed, 25 Oct 2017 23:27:47 +0300 Subject: [PATCH 3/4] Updated changelog --- Changelog.md | 2 ++ 1 file changed, 2 insertions(+) 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 From f72d5ed8e44e0f3c83f49f54105c5dccff0b4ce2 Mon Sep 17 00:00:00 2001 From: Victor Sharavara Date: Thu, 26 Oct 2017 00:02:34 +0300 Subject: [PATCH 4/4] Updated tests --- Tests/EndpointSpec.swift | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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)) } }