Skip to content

Instantly share code, notes, and snippets.

@Tarmil
Forked from Youenn-Bouglouan/Main.fs
Last active May 1, 2017 12:20
Show Gist options
  • Save Tarmil/fc685c412838d775f462b49b0d49e8f9 to your computer and use it in GitHub Desktop.
Save Tarmil/fc685c412838d775f462b49b0d49e8f9 to your computer and use it in GitHub Desktop.
Websharper - issue with POST expecting a JSON body
// Issue decribed here: http://websharper.com/question/82758/post-endpoint-with-a-json-body-cannot-be-reached
namespace HelloWebSharper
open WebSharper.Html.Server
open WebSharper
open WebSharper.Sitelets
module Site =
type EndPoint =
| [<EndPoint "GET /get">] Get
| [<EndPoint "POST /post_query"; Query "query">] PostQuery of query: string
| [<EndPoint "POST /post_body"; Json "body">] PostBody of body: JsonBody
| [<EndPoint "POST /post_simple">] PostSimple
and JsonBody = { test: string; }
[<Website>]
let Main =
let mainWebsite = Application.MultiPage (fun context action ->
match action with
| EndPoint.Get -> Content.Json ("Get works!")
| EndPoint.PostQuery query -> Content.Json ("PostQuery works!")
| EndPoint.PostBody body -> Content.Json ("PostBody works!")
| EndPoint.PostSimple -> Content.Json ("PostSimple works!")
)
Sitelet.Sum [ mainWebsite ]
// Run the server as a console application using Owin
module SelfHostedServer =
open global.Owin
open Microsoft.Owin.Hosting
open Microsoft.Owin.StaticFiles
open Microsoft.Owin.FileSystems
open WebSharper.Owin
open System
open System.Collections.Generic
open System.Threading.Tasks
type Greeting = { text: string }
type AppFunc = Func<IDictionary<string, obj>, Task>
let awaitTask = Async.AwaitIAsyncResult >> Async.Ignore
type Startup() =
member this.Configuration(app: IAppBuilder) =
// // Log info about incoming requests to the console
app.Use(fun environment next ->
async {
printfn "------ New request ------"
printfn " Path: %s" (environment.Request.Path.ToString())
printfn " Verb: %s" (environment.Request.Method)
// Copy the Body into a seekable stream if necessary, and reset its position.
if not environment.Request.Body.CanSeek then
let s = new MemoryStream() :> Stream
do! awaitTask <| environment.Request.Body.CopyToAsync s
environment.Request.Body <- s // So that WebSharper sees our MemoryStream
environment.Request.Body.Seek(0L, SeekOrigin.Begin) |> ignore
use reader = new System.IO.StreamReader(environment.Request.Body)
let body = reader.ReadToEnd()
printfn " Body: %s" (body)
// Reset the stream's position again after reading it.
// (note: we could fix WebSharper.Owin to do it itself, so that this isn't necessary)
environment.Request.Body.Seek(0L, SeekOrigin.Begin) |> ignore
do! awaitTask <| next.Invoke()
printfn " HTTP Status code: %i" environment.Response.StatusCode
if environment.Response.StatusCode <> 200 then
use writer = new System.IO.StreamWriter(environment.Response.Body)
writer.WriteLine("an error occurred!")
writer.Flush |> ignore
} |> Async.StartAsTask :> Task) |> ignore
// Load our site
app.UseStaticFiles(
StaticFileOptions(
FileSystem = PhysicalFileSystem("..")))
.UseSitelet("..", Site.Main)
|> ignore
[<EntryPoint>]
let main argv =
let uri = "http://localhost:9000"
use app = WebApp.Start<Startup>(uri)
printfn "Serving %s" uri
Console.ReadKey() |> ignore
printfn "Stopping %s" uri
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment