wreq如何结合文件上传和邮政表格字段?

时间:2019-07-15 16:41:48

标签: haskell wreq

如何将Part[(ByteString, ByteString)](普通形式输入)结合在一起?

partFile :: Text -> FilePath -> Part

它们都是Postable类型类http://hackage.haskell.org/package/wreq-0.5.3.2/docs/Network-Wreq-Types.html#t:Postable

的一部分

我猜想这是可以用镜头组合器完成的事情吗?

下面是我发送两个请求的完整代码-一个带有发布表单字段-另一个带有文件上传

app.cabal

cabal-version: 1.12    
name:           app
version:        0.1.0.0
author:         CabalSaneDefault
maintainer:     nobody
license:        BSD3
build-type:     Simple

executable app-test
  main-is: Test.hs
  other-modules:
      Paths_app
  hs-source-dirs:
      src
  build-depends:
      base
    , bytestring
    , lens
    , wreq
  default-language: Haskell2010

src / Test.hs

{-# Language OverloadedStrings #-}
module Test where

import Network.Wreq
import Control.Lens
import Data.ByteString (ByteString)
import Data.ByteString.Lazy.Char8 (putStrLn)
import qualified Network.Wreq.Session as S

main :: IO ()
main = do
  let rootUrl = "http://localhost:80"
  print "test"
  sess <- S.newSession
  x <- S.customHistoriedPayloadMethodWith
    "POST"
    defaults
    sess
    (rootUrl ++ "/post")
    ([("test","chris"), ("test2", "chris2")] :: [(ByteString, ByteString)])
  Data.ByteString.Lazy.Char8.putStrLn $ (x ^. hrFinalResponse ^. responseBody)
  print "--------------------------------------------------"
  x' <- S.customHistoriedPayloadMethodWith
    "POST"
    defaults
    sess
    (rootUrl ++ "/post")
    (
      (partFile "" "app.cabal")
    )
  Data.ByteString.Lazy.Char8.putStrLn $ (x' ^. hrFinalResponse ^. responseBody)

1 个答案:

答案 0 :(得分:0)

http://hackage.haskell.org/package/http-client-0.6.4/docs/Network-HTTP-Client-MultipartFormData.html#v:partBSPart创建ByteString,然后我们可以发布[Part],因为它是Postable的实例。

  x' <- S.customHistoriedPayloadMethodWith
    "POST"
    defaults
    sess
    (rootUrl ++ "/post")
    (
      [
        (partFile "" "app.cabal")
      , partBS "test" "chris"
      , partBS "test2" "chris2"
    ]
    )