9 people like it.

CORS response with Suave

A little website that lets you make CORS requests to it (update to the latest Suave version)

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
// Run as console app or fsx
open Suave
open Suave.Operators
open Suave.Filters
open Suave.Writers
open Suave.Successful

let setCORSHeaders =
    setHeader  "Access-Control-Allow-Origin" "*"
    >=> setHeader "Access-Control-Allow-Headers" "content-type"

let allow_cors : WebPart =
    choose [
        OPTIONS >=>
            fun context ->
                context |> (
                    setCORSHeaders
                    >=> OK "CORS approved" )
    ]

let webSite =
    choose [
        allow_cors
        GET >=> OK "URLs are for wimps. GETting something? This is what you get."
    ]

startWebServer defaultConfig webSite

(*
=== A fiddler scratchpad to test

OPTIONS http://localhost:8083/ HTTP/1.1
User-Agent: Fiddler
Origin: http://www.example-social-network.com
Host: localhost:8083
*)
// See the 'F# Tutorial' project for more help.
namespace Suave
module Operators

from Suave
module Filters

from Suave
module Writers

from Suave
module Successful

from Suave
val setCORSHeaders : (HttpContext -> Async<HttpContext option>)

Full name: Script.setCORSHeaders
val setHeader : key:string -> value:string -> WebPart

Full name: Suave.Writers.setHeader
val allow_cors : WebPart

Full name: Script.allow_cors
Multiple items
module WebPart

from Suave

--------------------
type WebPart = WebPart<HttpContext>

Full name: Suave.Http.WebPart

--------------------
type WebPart<'a> = 'a -> Async<'a option>

Full name: Suave.WebPart.WebPart<_>
val choose : options:WebPart<'a> list -> WebPart<'a>

Full name: Suave.WebPart.choose
val OPTIONS : WebPart

Full name: Suave.Filters.OPTIONS
val context : HttpContext
val OK : body:string -> WebPart

Full name: Suave.Successful.OK
val webSite : WebPart<HttpContext>

Full name: Script.webSite
val GET : WebPart

Full name: Suave.Filters.GET
val startWebServer : config:SuaveConfig -> webpart:WebPart -> unit

Full name: Suave.Web.startWebServer
val defaultConfig : SuaveConfig

Full name: Suave.Web.defaultConfig

More information

Link:http://fssnip.net/mL
Posted:8 years ago
Author:vilinski
Tags: suave , cors