Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

ocramz
Copy link

@ocramz ocramz commented Jan 1, 2024

closes #76

@ocramz ocramz mentioned this pull request Jan 1, 2024
5 tasks
@ocramz
Copy link
Author

ocramz commented Jan 11, 2024

Hi @sol , how do you do? What do you think of this patch? I'd find it very useful for testing scotty, so I hope you'll accept it here. Thanks!

@ocramz
Copy link
Author

ocramz commented Jan 28, 2024

@sol ping!

2 similar comments
@ocramz
Copy link
Author

ocramz commented Feb 27, 2024

@sol ping!

@ocramz
Copy link
Author

ocramz commented Aug 18, 2024

@sol ping!

Copy link
Member

@sol sol left a 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
}

@ocramz
Copy link
Author

ocramz commented Aug 19, 2024

I appreciate the feedback, bikeshedding is part of what makes OSS great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add file upload utilities
2 participants