代码之家  ›  专栏  ›  技术社区  ›  Alejandro Alcalde

无法将预期的类型请求与实际类型[Char]匹配

  •  1
  • Alejandro Alcalde  · 技术社区  · 5 年前

    我是Haskel的新手,我正在尝试使用 http-conduit ,这是他们的 documentation .

    然而,在运行程序时,我总是得到:

        • Couldn't match expected type ‘Request’ with actual type ‘[Char]’
        • In the first argument of ‘httpLBS’, namely
            ‘"http://httpbin.org/get"’
          In a stmt of a 'do' block:
            response <- httpLBS "http://httpbin.org/get"
          In the expression:
            do response <- httpLBS "http://httpbin.org/get"
               putStrLn
                 $ "The status code was: " ++ show (getResponseStatusCode response)
               print $ getResponseHeader "Content-Type" response
               L8.putStrLn $ getResponseBody response
       |
    12 |     response <- httpLBS "http://httpbin.org/get"
       |                         ^^^^^^^^^^^^^^^^^^^^^^^^
    

    我试图创建一个既有阴谋集团又有stack的项目 http管道 和 aeson 作为依赖项,但仍然会出现错误。

    url不应该隐式转换为 Request ?

    我试着导入 要求 试着创造一个 要求 从url,但它抱怨:

    import Network.HTTP.Client.Request
    
    <no location info>: error:
        Could not load module ‘Network.HTTP.Client.Request’
        it is a hidden module in the package ‘http-client-0.6.4.1’
    
    1 回复  |  直到 5 年前
        1
  •  3
  •   willeM_ Van Onsem    5 年前

    您需要启用 OverloadedStrings extension [schoolofhaskell] .你可以添加 LANGUAGE 将pragma添加到文件顶部,因此:

    {-# LANGUAGE OverloadedStrings #-}
    
    -- …

    此扩展将隐式添加 fromString :: IsString a => String -> a 每根绳子 字面意义的 (不要与带有 类型 String )。它使处理类似字符串的数据更加方便,例如 Text .

    然而,请注意,转换是在 运行时 ,如果不是全部 一串 s映射到a(有效) Request 对象,则只有当 要求 进行了评估。

    url不应该隐式转换为请求吗?

    在Haskell中,没有隐式转换,总是通过函数转换数据。这个 重载字符串 只需将隐式函数调用添加到 字面量 ,这意味着字符串文字现在可以将 IsString type class .

    推荐文章