You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am using servant-client to request JSON information from the hackage API, but it turns out that specifying '[JSON] in the Get request will not add a header Accept: application/json to the Request, even though requestAccept seems to have the correct application types.
Here's a reproducer:
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveAnyClass #-}
moduleMainwhereimportGHC.GenericsimportData.ProxyimportData.AesonimportServant.APIimportNetwork.HTTP.Client (newManager, defaultManagerSettings, requestHeaders)
importServant.ClientdataPackage=Package{packageName::String
, downloads::Int}derivingstockShowderivingstockGenericderivinganyclassFromJSONtypeAPI="top":>Get '[JSON] [Package]
getTop::ClientM [Package]
getTop = client (Proxy@API)
main::IO()
main =do
manager' <- newManager defaultManagerSettings
let clientEnv = mkClientEnv manager' (BaseUrlHttp"hackage.haskell.org"80"packages")
res <- runClientM getTop (clientEnv{makeClientRequest =\b r -> makeClientRequest clientEnv b r >>=\r' ->return r'{requestHeaders = [("Accept", "application/json")]}})
case res ofLeft err ->putStrLn$"Error: "++show err
Right packages ->print packages
with build-depends: base, servant, servant-client, aeson, http-client
Expected behaviour
I was expecting the Accept: application/json to be automatically added to the request, since I'm using a Get request and JSON as the decoding format.
The text was updated successfully, but these errors were encountered:
PS: download numbers on Hackage are unreliable because it doesn't use the data from its CDN, so the numbers are not representative at all of any kind of trend (except perhaps that a package has been downloaded at least once).
I don't recall if I analyzed the request generated by servant. Perhaps one could look to see whether Get '[JSON] [Package] is sufficient to add the correct header to the request.
I see now that my reproducer may be wrong in the sense that it is the one that works. If you remove the part where requestHeaders is manually edited to introduce the needed header, does it still work?
I am using servant-client to request JSON information from the hackage API, but it turns out that specifying '[JSON] in the Get request will not add a header
Accept: application/json
to the Request, even thoughrequestAccept
seems to have the correct application types.Here's a reproducer:
with
build-depends: base, servant, servant-client, aeson, http-client
Expected behaviour
I was expecting the
Accept: application/json
to be automatically added to the request, since I'm using aGet
request andJSON
as the decoding format.The text was updated successfully, but these errors were encountered: