Skip to content

Instantly share code, notes, and snippets.

@yodiz
Created July 7, 2022 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yodiz/6c7631ffc897a455185eb401e5fe62be to your computer and use it in GitHub Desktop.
Save yodiz/6c7631ffc897a455185eb401e5fe62be to your computer and use it in GitHub Desktop.
let private mapRequest (context : Microsoft.AspNetCore.Http.HttpContext) : Svea.Dry.Http.HttpRequest =
if context.Request.HasFormContentType then
let newStream : System.IO.Stream =
let ms = new System.IO.MemoryStream()
use sw = new System.IO.StreamWriter(ms, System.Text.Encoding.UTF8, 4096, true)
let mutable isFirst = true
for item in context.Request.Form do
sw.Write(sprintf "%s=%s" (System.Web.HttpUtility.UrlEncode(item.Key)) (item.Value |> Seq.map (fun x -> System.Web.HttpUtility.UrlEncode(x)) |> String.concat ","))
if isFirst then
isFirst <- false
sw.Write("&")
sw.Flush()
ms.Seek(0L, System.IO.SeekOrigin.Begin) |> ignore
ms
context.Request.Body <- newStream
@shazmodan
Copy link

Det verkar som att den bara klarar av 1-2 parametrar p.g.a "isFirst". Övriga parametrar kommer att tappa sin skiljare "&". Här är möjligtvis en bättre lösning:

if context.Request.HasFormContentType then
    let newStream : System.IO.Stream = 
        let ms = new System.IO.MemoryStream()
        use sw = new System.IO.StreamWriter(ms, System.Text.Encoding.UTF8, 4096, true)

        let lastIndex = context.Request.Form.Count - 1

        context.Request.Form
        |> Seq.iteri(fun index item -> 
            sw.Write(sprintf "%s=%s" (System.Web.HttpUtility.UrlEncode(item.Key)) (item.Value |> Seq.map (fun x -> System.Web.HttpUtility.UrlEncode(x)) |> String.concat ","))
            if not (index = lastIndex) then
                sw.Write("&")
            )

        sw.Flush()
        ms.Seek(0L, System.IO.SeekOrigin.Begin) |> ignore
        ms
    context.Request.Body <- newStream
    
    ()

@yodiz
Copy link
Author

yodiz commented Jul 18, 2022

Så korkad jag är, tänkte fel med &:andeet :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment