Recipe: Typed REST API

Goal: Route with typed path segments and return JSON-ish text.

open Suave
open Suave.Filters
open Suave.Operators
open Suave.Successful
open Suave.Writers

let app = choose [
  GET >=> pathScan "/users/%d" (fun id ->
    OK (sprintf """{"id":%d,"name":"Ada"}""" id)
    >=> setMimeType "application/json; charset=utf-8")
  GET >=> path "/users" >=> OK """[]""" >=> setMimeType "application/json; charset=utf-8"
  RequestErrors.NOT_FOUND "Not found"
]

startWebServer defaultConfig app

See Routing and the JSON recipe.