如何在Network.HTTP.Client中使用Data.Default

时间:2014-09-22 08:58:13

标签: haskell http-conduit

aws包使用默认请求的实例(在Aws / Core.hs中)。在构建程序包期间,编译器会抱怨与下面相同的错误。

这是一个简单的事情,但却失败了:

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

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

编译器抱怨:

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)

但我可以看到实例是在http-client / Network / HTTP / Client / Request.hs中定义的:

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

这是来自" clean"环境:

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

如何使用Default with Request?或者更好的是我如何构建aws包?

修改:输出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

2 个答案:

答案 0 :(得分:1)

这应该有效:

{-# 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" }

我已在FP Complete's Haskell IDE发布了工作代码。函数defhost都包含在http-conduit包中。它给出了以下输出:

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)
}

此处需要注意的是,您不需要使用def Data.Default。已为def定义Request 记录在http-conduit包中,您应该使用该功能。

答案 1 :(得分:1)

我猜你安装了两个不同版本的data-defaultdata-default-classRequest是其中一个类的实例,但不是另一个类的实例。遗憾的是,这是一个非常常见的问题,您可以看到a similar answer I provided on Reddit