haskell - 无法将预期类型 ‘Request’ 与实际类型 ‘[Char]’ 匹配

标签 haskell http-conduit

我是 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"
   |                         ^^^^^^^^^^^^^^^^^^^^^^^^

我尝试使用 cabal 和 stack 创建一个项目,添加 http-conduitaeson 作为依赖项,但仍然收到错误。

url 不应该隐式转换为 Request 吗?

我尝试导入 Request 并尝试从网址创建 Request,但它提示:

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’

最佳答案

您需要启用OverloadedStrings extension [schoolofhaskell] 。您可以将 LANGUAGE 编译指示添加到文件顶部,因此:

{-# <b>LANGUAGE OverloadedStrings</b> #-}

-- …

此扩展将隐式添加 fromString :: IsString a => String -> a每个字符串文字(不要与类型String的表达式混淆)。它使得处理类似字符串的数据变得更加方便,例如 Text .

但是请注意,转换是在运行时完成的,因此如果不是所有String都映射到(有效)Request对象,那么只有在评估 Request 时您才会看到此内容。

Shouldn't the url be implicitly converted to a Request?

在 Haskell 中,没有隐式转换,您始终通过函数来​​转换数据。 OverloadedStrings 只是向文字添加了隐式函数调用,因此这意味着字符串文字现在可以采用属于 IsString type class 成员的任何类型作为类型。 .

关于haskell - 无法将预期类型 ‘Request’ 与实际类型 ‘[Char]’ 匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66775825/

相关文章:

c - FFI 导入的 MPI 常量的 GHCi 链接器错误(通过 c2hs)

haskell - 在给定路径中查找给定字符串

带有 GADT 的 Haskell 类型推断和类型变量的类型类约束

haskell - 为什么 httpJSON 失败,但 httpLBS 成功?

haskell - 范围类型声明可能吗?

haskell - 状态 Monad 在游戏中保存棋盘

haskell - 执行http-conduit 2.0.0.4版本的示例代码时如何修复错误

http - 增加 Network.HTTP.Conduit 的请求超时

http - 如何构造 Network.HTTP.Conduit.Request 对象?

java - SocketTimeoutException cxf httpconduit PUT