本文介绍了在ghci中使用Network.HTTP.Simple.httpJSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例正常工作。但为什么 httpJSON 的执行在 ghci 中不起作用?

The Receiving JSON example works fine. But why the execution of httpJSON doesn't work in ghci?

λ> :set -XOverloadedStrings
λ> import           Network.HTTP.Simple
λ> :t httpJSON "http://httpbin.org/get"
λ> x <- httpJSON "http://httpbin.org/get"

<interactive>:97:6: error:
    • Ambiguous type variable ‘a0’ arising from a use of ‘httpJSON’
      prevents the constraint ‘(aeson-1.0.2.1:Data.Aeson.Types.FromJSON.FromJSON
                                  a0)’ from being solved.
      Probable fix: use a type annotation to specify what ‘a0’ should be.
      These potential instances exist:
        instance aeson-1.0.2.1:Data.Aeson.Types.FromJSON.FromJSON Value
          -- Defined in ‘aeson-1.0.2.1:Data.Aeson.Types.FromJSON’
        instance (aeson-1.0.2.1:Data.Aeson.Types.FromJSON.FromJSON a,
                  aeson-1.0.2.1:Data.Aeson.Types.FromJSON.FromJSON b) =>
                 aeson-1.0.2.1:Data.Aeson.Types.FromJSON.FromJSON (Either a b)
          -- Defined in ‘aeson-1.0.2.1:Data.Aeson.Types.FromJSON’
        instance aeson-1.0.2.1:Data.Aeson.Types.FromJSON.FromJSON Ordering
          -- Defined in ‘aeson-1.0.2.1:Data.Aeson.Types.FromJSON’
        ...plus 24 others
        ...plus 53 instances involving out-of-scope types
        (use -fprint-potential-instances to see them all)
    • In the first argument of ‘GHC.GHCi.ghciStepIO ::
                                  forall a. IO a -> IO a’, namely
        ‘httpJSON "http://httpbin.org/get"’
      In a stmt of an interactive GHCi command:
        x <- GHC.GHCi.ghciStepIO :: forall a. IO a -> IO a
             (httpJSON "http://httpbin.org/get")


推荐答案

感谢@ user2407038的提示我想通了:

Thanks @user2407038's hint I figured out:

λ> :t liftM (\x -> getResponseBody x :: Value) (httpJSON "http://httpbin.org/get")
liftM (\x -> getResponseBody x :: Value) (httpJSON "http://httpbin.org/get")
  :: MonadIO m => m Value                                           
λ> liftM (\x -> getResponseBody x :: Value) (httpJSON "http://httpbin.org/get")
Object (fromList [("origin",String "158.181.77.67"),("args",Object (fromList [])),("url",String "http://httpbin.org/get"),("headers",Obj
g "close"),("Host",String "httpbin.org")]))])

这篇关于在ghci中使用Network.HTTP.Simple.httpJSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 18:04