-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add postMultipartForm #77
base: main
Are you sure you want to change the base?
Conversation
Hi @sol , how do you do? What do you think of this patch? I'd find it very useful for testing |
@sol ping! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
}
I appreciate the feedback, bikeshedding is part of what makes OSS great! |
closes #76