0

我需要添加一个标头并发送一个请求:

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

4

1 回答 1

3

正如错误所暗示的那样,编译器需要一个 type Data.CaseInsensitive.CI C8.ByteString,而您为它提供[Char](aka String)。

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

{-# LANGUAGE OverloadedStrings #-}
于 2014-07-07T12:12:52.877 回答