Skip to content

Commit

Permalink
Make sure that proper headers are passed before JSON HTTP call
Browse files Browse the repository at this point in the history
Since the functions parses the body as JSON, it is appropriate to set
the Accept header to be application/json indicating the server that
the client wants JSON in response.

Also application/json is recognized as a standard MIME media type:
https://tools.ietf.org/html/rfc4627#section-6

This patch will fix snoyberg#284
  • Loading branch information
psibi committed Jul 7, 2017
1 parent c9f541f commit 738fa65
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions http-conduit/Network/HTTP/Simple.hs
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,17 @@ httpJSON req = liftIO $ httpJSONEither req >>= T.mapM (either throwIO return)
httpJSONEither :: (MonadIO m, FromJSON a)
=> H.Request
-> m (H.Response (Either JSONException a))
httpJSONEither req =
liftIO $ httpSink req sink
httpJSONEither req = liftIO $ httpSink req' sink
where
req' = addRequestHeader H.hAccept "application/json" req
sink orig = fmap (\x -> fmap (const x) orig) $ do
eres1 <- C.sinkParserEither json'
case eres1 of
Left e -> return $ Left $ JSONParseException req orig e
Left e -> return $ Left $ JSONParseException req' orig e
Right value ->
case A.fromJSON value of
A.Error e -> return $ Left $ JSONConversionException
req (fmap (const value) orig) e
req' (fmap (const value) orig) e
A.Success x -> return $ Right x

-- | An exception that can occur when parsing JSON
Expand Down

0 comments on commit 738fa65

Please sign in to comment.