Skip to content

Commit

Permalink
Merge pull request #1410 from Vict0rS/encodable-content-type
Browse files Browse the repository at this point in the history
Added missing content type for requestJSONEncodable task
  • Loading branch information
sunshinejr committed Oct 28, 2017
2 parents 4935fe4 + f72d5ed commit f615ec0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 2 additions & 0 deletions 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
Expand Down
6 changes: 6 additions & 0 deletions Sources/Moya/URLRequest+Encoding.swift
Expand Up @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion Tests/EndpointSpec.swift
Expand Up @@ -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))
}
}
Expand Down

0 comments on commit f615ec0

Please sign in to comment.