How to use Data.Default with Network.HTTP.Client

The aws package uses the default request instance (in Aws / Core.hs). While building the package, the compiler complains about the same error as below.

Here's something simple that doesn't work the same way:

{-# LANGUAGE OverloadedStrings #-}
import Data.Default        (def)
import Network.HTTP.Client (host)

main :: IO ()
main = do
    print $ def { host = "http://foobar.com" } 

      

The compiler complains:

No instance for (Data.Default.Default
                   http-client-0.3.8.2:Network.HTTP.Client.Types.Request)
  arising from a use of `def'
Possible fix:
  add an instance declaration for
  (Data.Default.Default
     http-client-0.3.8.2:Network.HTTP.Client.Types.Request)

      

But I can see that the instance is defined in http-client / Network / HTTP / Client / Request.hs:

instance Default Request where
def = Request
    { host = "localhost"
    , port = 80
    , secure = False
...

      

This is from a "clean" environment:

$ cd
$ rm -Rf .cabal .ghc
$ cabal install cabal cabal-install
$ cabal install http-client-0.3.8.2

      

How do I use the Default With Prompt option? Or better yet, how can I build the aws package?

Edit: Output from ghc-pkg

$ ghc-pkg list 
/var/lib/ghc/package.conf.d
   Cabal-1.16.0
   GLURaw-1.3.0.0
   GLUT-2.4.0.0
   HTTP-4000.2.8
   HUnit-1.2.5.2
   MonadCatchIO-mtl-0.3.0.5
   OpenGL-2.8.0.0
   OpenGLRaw-1.3.0.0
   QuickCheck-2.6
   X11-1.6.1.1
   X11-xft-0.3.1
   array-0.4.0.1
   async-2.0.1.4
   attoparsec-0.10.4.0
   base-4.6.0.1
   bin-package-db-0.0.0.0
   binary-0.5.1.1
   bytestring-0.10.0.2
   case-insensitive-1.1.0.2
   cgi-3001.1.8.3
   containers-0.5.0.0
   data-default-0.5.1
   deepseq-1.3.0.1
   directory-1.2.0.1
   dlist-0.5
   extensible-exceptions-0.1.1.4
   fgl-5.4.2.4
   filepath-1.3.0.1
   ghc-7.6.3
   ghc-prim-0.3.0.0
   hashable-1.2.1.0
   haskell-src-1.0.1.5
   haskell2010-1.1.1.0
   haskell98-2.0.0.2
   hoopl-3.9.0.0
   hpc-0.6.0.0
   html-1.0.1.2
   integer-gmp-0.5.0.0
   mtl-2.1.2
   network-2.4.1.2
   old-locale-1.0.0.5
   old-time-1.1.0.1
   parallel-3.2.0.3
   parsec-3.1.3
   pretty-1.1.1.0
   primitive-0.5.0.1
   process-1.1.0.2
   random-1.0.1.1
   regex-base-0.93.2
   regex-compat-0.95.1
   regex-posix-0.95.2
   rts-1.0
   split-0.2.2
   stm-2.4.2
   syb-0.4.0
   template-haskell-2.8.0.0
   text-0.11.3.1
   time-1.4.0.1
   transformers-0.3.0.0
   unix-2.6.0.1
   unordered-containers-0.2.3.0
   utf8-string-0.3.7
   vector-0.10.0.1
   xhtml-3000.2.1
   xmonad-0.11
   xmonad-contrib-0.11.2
   zlib-0.5.4.1
/home/ben/.ghc/i386-linux-7.6.3/package.conf.d
   Cabal-1.20.0.2
   asn1-encoding-0.8.1.3
   asn1-parse-0.8.1
   asn1-types-0.2.3
   base64-bytestring-1.0.0.1
   blaze-builder-0.3.3.4
   byteable-0.1.1
   cereal-0.4.0.1
   cipher-aes-0.2.8
   cipher-des-0.0.6
   cipher-rc4-0.1.4
   conduit-1.2.0.2
   connection-0.2.3
   cookie-0.4.1.3
   cprng-aes-0.5.2
   crypto-cipher-types-0.0.9
   crypto-numbers-0.2.3
   crypto-pubkey-0.2.4
   crypto-pubkey-types-0.4.2.2
   crypto-random-0.0.8
   cryptohash-0.11.6
   data-default-class-0.0.1
   exceptions-0.6.1
   http-client-0.4.0
   http-client-tls-0.2.2
   http-conduit-2.1.4.3
   http-types-0.8.5
   lifted-base-0.2.3.0
   mime-types-0.1.0.4
   mmorph-1.0.4
   monad-control-0.3.3.0
   nats-0.2
   pem-0.2.2
   publicsuffixlist-0.1
   resourcet-1.1.2.3
   securemem-0.1.3
   semigroups-0.15.3
   socks-0.5.4
   streaming-commons-0.1.5
   tls-1.2.9
   transformers-base-0.4.3
   void-0.6.1
   x509-1.4.12
   x509-store-1.4.4
   x509-system-1.4.5
   x509-validation-1.5.0

      

+3


source to share


2 answers


I am assuming you have two different versions installed, data-default

or data-default-class

. Request

is an instance of one of these classes, but not the other. This is unfortunately a very common problem, you can see a similar answer that I posted on Reddit .



+1


source


This should work:

{-# LANGUAGE OverloadedStrings #-}

-- | Main entry point to the application.
module Main where

import Network.HTTP.Conduit (def,host)

main :: IO ()
main = putStrLn $ show $ def { host = "http://foobar.com" }

      

I posted working code FP Complete Haskell IDE . Both functions def

and host

are contained in the package http-conduit

. It gives the following output:



Request {
  host                 = "http://foobar.com"
  port                 = 80
  secure               = False
  clientCertificates   = []
  requestHeaders       = []
  path                 = "/"
  queryString          = ""
  requestBody          = RequestBodyLBS Empty
  method               = "GET"
  proxy                = Nothing
  rawBody              = False
  redirectCount        = 10
  responseTimeout      = Just (-3425)
}

      

It should be noted that you do not need to use def

from Data.Default

. def

already defined for Request

write to package http-conduit

and you must use this function.

+1


source







All Articles