由于使用“ req”而产生的类型变量“ a0”不明确

时间:2019-09-17 08:37:09

标签: haskell req

我正在尝试使用req Haskell软件包从端点检索响应。

我知道我需要使用类型注释来指定“ a0”应该是什么,但是我不确定“ a0”是指什么以及应该给它什么类型。

{-# LANGUAGE OverloadedStrings #-}
module Main where

import Control.Monad.IO.Class
import Data.Aeson
import Network.HTTP.Req

main :: IO ()
main = runReq defaultHttpConfig $ do
  r <- req GET
    (https "google.com")
    NoReqBody
    jsonResponse
    mempty
  liftIO $ print jsonResponse

完整追溯:

/Users/timothy/repos/uptime-bot/app/Main.hs:10:14: error:
    • Ambiguous type variable ‘a0’ arising from a use of ‘req’
      prevents the constraint ‘(FromJSON a0)’ from being solved.
      Probable fix: use a type annotation to specify what ‘a0’ should be.
      These potential instances exist:
        instance FromJSON DotNetTime
          -- Defined in ‘aeson-1.4.5.0:Data.Aeson.Types.FromJSON’
        instance FromJSON Value
          -- Defined in ‘aeson-1.4.5.0:Data.Aeson.Types.FromJSON’
        instance (FromJSON a, FromJSON b) => FromJSON (Either a b)
          -- Defined in ‘aeson-1.4.5.0:Data.Aeson.Types.FromJSON’
        ...plus 25 others
        ...plus 63 instances involving out-of-scope types
        (use -fprint-potential-instances to see them all)
    • In a stmt of a 'do' block:
        r <- req GET (https "google.com") NoReqBody jsonResponse mempty
      In the second argument of ‘($)’, namely
        ‘do r <- req GET (https "google.com") NoReqBody jsonResponse mempty
            liftIO $ print jsonResponse’
      In the expression:
        runReq defaultHttpConfig
          $ do r <- req
                      GET (https "google.com") NoReqBody jsonResponse mempty
               liftIO $ print jsonResponse
   |
10 |         r <- req GET
   |              ^^^^^^^^...

1 个答案:

答案 0 :(得分:1)

像这样解决它:

main :: IO ()
main = runReq defaultHttpConfig $ do
  r <- req GET (https "google.com") NoReqBody jsonResponse mempty
  liftIO $ print (responseBody r :: Value)