我需要添加一个标题并发送一个请求:
import Network.HTTP.Conduit
import qualified Data.ByteString.Char8 as C8
--..........
res <- withManager $ httpLbs $ createReq request
return ()
where
createReq r = r {
--...........
requestHeaders = ("content-type", "application/json") : requestHeaders r
}
我有两个错误:
Couldn't match type `[Char]'
with `case-insensitive-1.0.0.1:Data.CaseInsensitive.CI
C8.ByteString'
Expected type: HeaderName
Actual type: [Char]
In the expression: "content-type"
In the first argument of `(:)', namely
`("content-type", "application/json")'
In the `requestHeaders' field of a record
Couldn't match expected type `C8.ByteString'
with actual type `[Char]'
In the expression: "application/json"
In the first argument of `(:)', namely
`("content-type", "application/json")'
我如何解决它们?
:C8.pack
没有,它会导致其他错误
正如错误提示的那样,编译器期望的类型是Data.CaseInsensitive.CI C8.ByteString
,而您提供的类型是[Char]
(又名String
)。
我怀疑您的问题是由缺少OverloadedStrings
扩展引起的,该扩展允许从字符串字面量构造任意类型。要解决这个问题,在模块的开头添加以下行:
{-# LANGUAGE OverloadedStrings #-}