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: 
38: 
// Run as console app or fsx
open Suave.Http
open Suave.Types
open Suave.Http.Successful
open Suave.Http.Writers
open Suave.Http.Applicatives
open Suave.Web

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 Http

from Suave
module Web

from Suave
val setCORSHeaders : obj

Full name: Script.setCORSHeaders
val allow_cors : WebPart

Full name: Script.allow_cors
type WebPart = Suave.WebPart.WebPart<HttpContext>

Full name: Suave.Http.WebPart
union case HttpMethod.OPTIONS: HttpMethod
val context : apply:(HttpContext -> HttpContext -> 'a) -> context:HttpContext -> 'a

Full name: Suave.Http.context
val webSite : WebPart

Full name: Script.webSite
union case HttpMethod.GET: HttpMethod
val startWebServer : config:Suave.SuaveConfig -> webpart:WebPart -> unit

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

Full name: Suave.Web.defaultConfig

More information

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