询问如何将文件上传与帖子表单字段相结合



如何将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

的一部分

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

下面的完整代码,我将在其中发送两个请求 - 一个带有帖子表单字段 - 另一个带有文件上传。

苹果公司

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)
http://hackage.haskell.org/package/http-client-0.6.4/docs/Network-HTTP-Client-MultipartFormData.html#v:partBS 从

ByteString创建Part,然后我们可以发布[Part],因为它是Postable的实例。

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

相关内容

  • 没有找到相关文章

最新更新