Modulehttp-client-0.7.19Haskell2010
Network.HTTP.Client.MultipartFormData
This module handles building multipart/form-data. Example usage:
{-# LANGUAGE OverloadedStrings #-}
import Network
import Network.HTTP.Client
import Network.HTTP.Client.MultipartFormData
import Data.Text.Encoding as TE
import Control.Monad
main = void $ withManager defaultManagerSettings $ \m -> do
req1 <- parseRequest "http://random-cat-photo.net/cat.jpg"
res <- httpLbs req1 m
req2 <- parseRequest "http://example.org/~friedrich/blog/addPost.hs"
flip httpLbs m =<<
(formDataBody [partBS "title" "Bleaurgh"
,partBS "text" $ TE.encodeUtf8 "矢田矢田矢田矢田矢田"
,partFileSource "file1" "/home/friedrich/Photos/MyLittlePony.jpg"
,partFileRequestBody "file2" "cat.jpg" $ RequestBodyLBS $ responseBody res]
req2)Synopsis
- type Part
- data PartM
- value partName
- value partFilename
- value partContentType
- value partHeaders
- value partGetBody
- value partBS
- value partLBS
- value partFile
- value partFileSource
- value partFileSourceChunked
- value partFileRequestBody
- value partFileRequestBodyM
- value addPartHeaders
- value formDataBody
- value formDataBodyWithBoundary
- value webkitBoundary
- value webkitBoundaryPure
- value renderParts
- value renderPart
- Packagehttp-client-0.7.19
- Exports21
- LanguageHaskell2010
- LicenceMIT
- SourceMultipartFormData.hs
Part type
A single part of a multipart message.
Name of the corresponding <input>
A file name, if this is an attached file
Content type
List of additional headers
Action in m which returns the body of a message.
Constructing parts
partBS :: Applicative m=> TextName of the corresponding <input>.
-> ByteStringThe body for this Part.
-> PartM m
Make a Part whose content is a strict ByteString.
The Part does not have a file name or content type associated with it.
partLBS :: Applicative m=> TextName of the corresponding <input>.
-> ByteStringThe body for this Part.
-> PartM m
Make a Part whose content is a lazy ByteString.
The Part does not have a file name or content type associated with it.
partFile Make a Part from a file.
The entire file will reside in memory at once. If you want constant memory usage, use partFileSource.
The FilePath supplied will be used as the file name of the Part. If you do not want to reveal this name to the server, you must remove it prior to uploading.
The Part does not have a content type associated with it.
partFileSource partFileSourceChunked will read a file and send it in chunks.
Note that not all servers support this. Only use partFileSourceChunked if you know the server you're sending to supports chunked request bodies.
The FilePath supplied will be used as the file name of the Part. If you do not want to reveal this name to the server, you must remove it prior to uploading.
The Part does not have a content type associated with it.
partFileRequestBody :: Applicative m=> TextName of the corresponding <input>.
-> FilePathFile name to supply to the server.
-> RequestBodyData to upload.
-> PartM m
Construct a Part from form name, filepath and a RequestBody
partFileRequestBody "who_calls" "caller.json" $ RequestBodyBS "{\"caller\":\"Jason J Jason\"}"-- empty upload form
partFileRequestBody "file" mempty memptyThe Part does not have a content type associated with it.
partFileRequestBodyM :: TextName of the corresponding <input>.
-> FilePathFile name to supply to the server.
-> m RequestBodyAction that will supply data to upload.
-> PartM m
Construct a Part from action returning the RequestBody
partFileRequestBodyM "cat_photo" "haskell-the-cat.jpg" $ do
size <- fromInteger <$> withBinaryFile "haskell-the-cat.jpg" ReadMode hFileSize
return $ RequestBodySource size $ CB.sourceFile "haskell-the-cat.jpg" $= CL.map fromByteStringThe Part does not have a content type associated with it.
Headers
Add a list of additional headers to this Part.
Building form data
Add form data to the Request.
This sets a new requestBody, adds a content-type request header and changes the method to POST.
Add form data with supplied boundary
Boundary
Generate a boundary simillar to those generated by WebKit-based browsers.
Misc
Combine the Parts to form multipart/form-data body