wreq 如何将文件上传与 post 表单域结合起来?
wreq how to combine file upload with post form fields?
如何将 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
的一部分
我猜是不是可以用镜头组合器来完成?
完整代码如下,我发送两个请求 - 一个带有 post 表单字段 - 另一个带有文件上传。
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)
http://hackage.haskell.org/package/http-client-0.6.4/docs/Network-HTTP-Client-MultipartFormData.html#v:partBS 从 ByteString
创建 Part
然后我们可以 post 一个 [Part]
因为它是 Postable
的实例.
x' <- S.customHistoriedPayloadMethodWith
"POST"
defaults
sess
(rootUrl ++ "/post")
(
[
(partFile "" "app.cabal")
, partBS "test" "chris"
, partBS "test2" "chris2"
]
)
如何将 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
我猜是不是可以用镜头组合器来完成?
完整代码如下,我发送两个请求 - 一个带有 post 表单字段 - 另一个带有文件上传。
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)
http://hackage.haskell.org/package/http-client-0.6.4/docs/Network-HTTP-Client-MultipartFormData.html#v:partBS 从 ByteString
创建 Part
然后我们可以 post 一个 [Part]
因为它是 Postable
的实例.
x' <- S.customHistoriedPayloadMethodWith
"POST"
defaults
sess
(rootUrl ++ "/post")
(
[
(partFile "" "app.cabal")
, partBS "test" "chris"
, partBS "test2" "chris2"
]
)