FSharp Suave - How to set cookies in the response?

72 Views Asked by At

I have a simple HTTP server configured in Suave as follows.

open System
open Suave
open Suave.Operators
open Suave.Filters
open Suave.Successful

[<EntryPoint>]
let main argv =
    
    let httpPort = 9000

    let httpConfig = {
        defaultConfig with bindings = [ HttpBinding.createSimple HTTP "0.0.0.0" httpPort ]
    }

    let httpApp = 
        choose

            [ POST >=> choose
        
                [ 
                    path "/login" >=> 
                        request (fun ctx -> 
                            let sessionId = someLoginLogic
                        ) 
                ]
        
            ]

    startWebServer httpConfig httpApp

Here, I have a login endpoint that will generate a random session id and I want to set that session id in cookies with HttpOnly and Secure flags.

1

There are 1 best solutions below

0
Brian Berns On

The typical way to set cookies in Suave is via its cookie state store, as documented here:

statefulForSession
   >=> setSessionValue "sessionId" someLoginLogic

However, that doesn't give you a way to explicitly set the cookie's secure and httpOnly flags. If you want to do that, I think you can do it manually like this:

statefulForSession
  >=> Suave.Cookie.setCookie (
    HttpCookie.create "sessionId" someLoginLogic None None None true true None)

Related Questions in F#