Last active
May 1, 2017 12:14
-
-
Save Youenn-Bouglouan/55cbb0257a9884d5d62c01b2025d27fc to your computer and use it in GitHub Desktop.
Websharper - issue with POST expecting a JSON body
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 -> | |
printfn "------ New request ------" | |
printfn " Path: %s" (environment.Request.Path.ToString()) | |
printfn " Verb: %s" (environment.Request.Method) | |
// If we comment the next 3 lines, the POST with body will work! | |
use reader = new System.IO.StreamReader(environment.Request.Body) | |
let body = reader.ReadToEnd() | |
printfn " Body: %s" (body) | |
async { | |
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