Skip to content

Commit 484bc9c

Browse files
authored
Merge pull request #669 from phadej/json-with-charset
Change JSON content type to add the charset
2 parents 6d0aa92 + 7793b52 commit 484bc9c

File tree

4 files changed

+14
-6
lines changed

4 files changed

+14
-6
lines changed

servant-server/test/Servant/ServerSpec.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ verbSpec = describe "Servant.API.Verb" $ do
173173
it "sets the Content-Type header" $ do
174174
response <- THW.request method "" [] ""
175175
liftIO $ simpleHeaders response `shouldContain`
176-
[("Content-Type", "application/json")]
176+
[("Content-Type", "application/json;charset=utf-8")]
177177

178178
test "GET 200" get200 methodGet 200
179179
test "POST 210" post210 methodPost 210

servant/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
([#345](https://github.com/haskell-servant/servant/pull/345))
1313
([#305](https://github.com/haskell-servant/servant/issues/305))
1414

15+
* Default JSON content type change to `application/json;charset=utf-8`.
16+
([#263](https://github.com/haskell-servant/servant/issues/263))
17+
Related browser bugs:
18+
[Chromium](https://bugs.chromium.org/p/chromium/issues/detail?id=438464) and
19+
[Firefox](https://bugzilla.mozilla.org/show_bug.cgi?id=918742)
20+
1521
0.9.1
1622
------
1723

servant/src/Servant/API/ContentTypes.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ class Accept ctype where
130130

131131
-- | @application/json@
132132
instance Accept JSON where
133-
contentType _ = "application" M.// "json"
133+
contentTypes _ =
134+
"application" M.// "json" M./: ("charset", "utf-8") NE.:|
135+
[ "application" M.// "json" ]
134136

135137
-- | @application/x-www-form-urlencoded@
136138
instance Accept FormUrlEncoded where

servant/test/Servant/API/ContentTypesSpec.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,21 +115,21 @@ spec = describe "Servant.API.ContentTypes" $ do
115115

116116
it "returns the Content-Type as the first element of the tuple" $ do
117117
handleAcceptH (Proxy :: Proxy '[JSON]) "*/*" (3 :: Int)
118-
`shouldSatisfy` ((== "application/json") . fst . fromJust)
118+
`shouldSatisfy` ((== "application/json;charset=utf-8") . fst . fromJust)
119119
handleAcceptH (Proxy :: Proxy '[PlainText, JSON]) "application/json" (3 :: Int)
120-
`shouldSatisfy` ((== "application/json") . fst . fromJust)
120+
`shouldSatisfy` ((== "application/json;charset=utf-8") . fst . fromJust)
121121
handleAcceptH (Proxy :: Proxy '[PlainText, JSON, OctetStream])
122122
"application/octet-stream" ("content" :: ByteString)
123123
`shouldSatisfy` ((== "application/octet-stream") . fst . fromJust)
124124

125125
it "returns the appropriately serialized representation" $ do
126126
property $ \x -> handleAcceptH (Proxy :: Proxy '[JSON]) "*/*" (x :: SomeData)
127-
== Just ("application/json", encode x)
127+
== Just ("application/json;charset=utf-8", encode x)
128128

129129
it "respects the Accept spec ordering" $ do
130130
let highest a b c = maximumBy (compare `on` snd)
131131
[ ("application/octet-stream", a)
132-
, ("application/json", b)
132+
, ("application/json;charset=utf-8", b)
133133
, ("text/plain;charset=utf-8", c)
134134
]
135135
let acceptH a b c = addToAccept (Proxy :: Proxy OctetStream) a $

0 commit comments

Comments
 (0)