Skip to content

Commit

Permalink
Add support for plain control types
Browse files Browse the repository at this point in the history
Closes: #1193
  • Loading branch information
kant2002 committed Jul 15, 2023
1 parent 5088a47 commit dd3f913
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions src/FSharp.Data.Http/Http.fs
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,11 @@ module HttpStatusCodes =


type MultipartItem = MultipartItem of formField: string * filename: string * content: Stream
type MultipartFileItem = MultipartFileItem of formField: string * filename: string option * contentType: string option * content: Stream

type MultipartFormDataItem =
| FileValue of MultipartFileItem
| FormValue of string * string

/// The body to send in an HTTP request
type HttpRequestBody =
Expand All @@ -649,6 +654,9 @@ type HttpRequestBody =
/// A sequence of formParamName * fileName * fileContent groups
| Multipart of boundary: string * parts: seq<MultipartItem>

/// A sequence of formParamName * fileName * fileContent groups
| MultipartFormData of boundary: string * parts: seq<MultipartFormDataItem>

/// The response body returned by an HTTP request
type HttpResponseBody =
| Text of string
Expand Down Expand Up @@ -1521,7 +1529,7 @@ module internal HttpHelpers =
/// c) write newline
/// d) write section data
/// 3) write trailing boundary
let writeMultipart (boundary: string) (parts: seq<MultipartItem>) (e: Encoding) =
let writeMultipart (boundary: string) (parts: seq<MultipartFileItem>) (e: Encoding) =
let newlineStream () =
new MemoryStream(e.GetBytes "\r\n") :> Stream

Expand All @@ -1545,17 +1553,20 @@ module internal HttpHelpers =

let segments =
parts
|> Seq.map (fun (MultipartItem (formField, fileName, contentStream)) ->
let fileExt = Path.GetExtension fileName
let contentType = defaultArg (MimeTypes.tryFind fileExt) "application/octet-stream"
|> Seq.map (fun (MultipartFileItem(formField, fileName, contentType, contentStream)) ->
let printHeader (header, value) = sprintf "%s: %s" header value

let sharedHeaders = [
prefixedBoundary
HttpRequestHeaders.ContentDisposition("form-data", Some formField, fileName)
|> printHeader ]
let headers = match contentType with
| Some(contentType) ->
sharedHeaders
|> Seq.append [ HttpRequestHeaders.ContentType contentType |> printHeader ]
| None -> sharedHeaders
let headerpart =
[ prefixedBoundary
HttpRequestHeaders.ContentDisposition("form-data", Some formField, Some fileName)
|> printHeader
HttpRequestHeaders.ContentType contentType
|> printHeader ]
headers
|> String.concat "\r\n"

let headerStream =
Expand Down Expand Up @@ -2079,7 +2090,17 @@ type Http private () =
|> e.GetBytes

HttpContentTypes.FormValues, (fun e -> new MemoryStream(bytes e) :> _)
| Multipart (boundary, parts) -> HttpContentTypes.Multipart(boundary), writeMultipart boundary parts
| Multipart (boundary, parts) ->
let fileParts = parts |> Seq.map (fun (MultipartItem(formField, fileName, stream)) ->
let fileExt = Path.GetExtension fileName
let contentType = defaultArg (MimeTypes.tryFind fileExt) "application/octet-stream"
MultipartFileItem(formField, Some fileName, Some contentType, stream))
HttpContentTypes.Multipart(boundary), writeMultipart boundary fileParts
| MultipartFormData (boundary, parts) ->
let fileParts = parts |> Seq.map (fun p -> match p with
| FormValue(formField, value) -> MultipartFileItem(formField, None, None, new MemoryStream(Encoding.UTF8.GetBytes(value)))
| FileValue(item) -> item)
HttpContentTypes.Multipart(boundary), writeMultipart boundary fileParts

// Set default content type if it is not specified by the user
let encoding =
Expand Down

0 comments on commit dd3f913

Please sign in to comment.