1

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

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)
4

1 回答 1

0

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

  x' <- S.customHistoriedPayloadMethodWith
    "POST"
    defaults
    sess
    (rootUrl ++ "/post")
    (
      [
        (partFile "" "app.cabal")
      , partBS "test" "chris"
      , partBS "test2" "chris2"
    ]
    )
于 2019-07-16T11:37:42.650 回答