我是 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-conduit
和 aeson
作为依赖项,但仍然收到错误。
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/