无法在请求中附加标头

时间:2014-07-07 12:04:29

标签: haskell http-conduit

我需要添加标题并发送请求:

 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
      }

我有2个错误:

   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没有,它会导致其他错误。

1 个答案:

答案 0 :(得分:3)

正如错误所示,编译器需要一个Data.CaseInsensitive.CI C8.ByteString类型,而你提供[Char](又名String)。

我怀疑你的问题是由缺少的OverloadedStrings扩展引起的,它允许从字符串文字构造任意类型。要解决此问题,请在模块的开头添加以下行:

{-# LANGUAGE OverloadedStrings #-}