Add postMultipartForm - #77
Open
ocramz wants to merge 1 commit into
Open
Conversation
5 tasks
Author
|
Hi @sol , how do you do? What do you think of this patch? I'd find it very useful for testing |
Author
|
@sol ping! |
sol
reviewed
Aug 19, 2024
Member
There was a problem hiding this comment.
Unless you're willing to get this bikeshedded to death, a separate package (e.g. hspec-wai-multipart) might be a better option for this.
But if you're inclined to get this upstream, this could be a start:
data FormData = FormData {
boundary :: ByteString
, parts :: [(String, Part)]
}
data Part = Part {
filename :: Maybe FilePath
, contentType :: Maybe MimeType
, body :: ByteString
}
instance IsString Part where
fromString = Part Nothing Nothing . encodeUtf8
filePart :: FilePath -> ByteString -> Part
filePart name body = Part (Just name) (Just . defaultMimeLookup $ fromString name) body
postMultipartForm :: ByteString -> [(String, Part)] -> WaiSession st SResponse
postMultipartForm path = postFormData path . FormData defaultBoundary
defaultBoundary :: ByteString
defaultBoundary = "..." -- some (base 64?) encoded randomness from random.org that is large enough so that it is effectively unique
postFormData :: ByteString -> FormData -> WaiSession st SResponse
postFormData = ...Usage:
postMultipartForm "/update-profile"
[("name", "Joe Doe"), ("picture", filePart "me.jpg" imageData)]Note that Part could also be
data Part = Part {
filename :: Maybe FilePath
, contentType :: Maybe MimeType
, headers :: [Header]
, body :: ByteString
}or even
data Part = Part {
filename :: Maybe FilePath
, headers :: [Header]
, body :: ByteString
}
Author
|
I appreciate the feedback, bikeshedding is part of what makes OSS great! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
closes #76