Building RESTful Web Services

We can expose simple REST web services with the help of the combinator mapJson. The mapJson uses the default BCL JSON serializer DataContractJsonSerializer.

open Suave open Suave.Json open System.Runtime.Serialization [<DataContract>] type Foo = { [<field: DataMember(Name = "foo")>] foo : string } [<DataContract>] type Bar = { [<field: DataMember(Name = "bar")>] bar : string } startWebServer defaultConfig (mapJson (fun (a:Foo) -> { bar = a.foo }))


ademar@nascio:~$ curl -X POST -d '{"foo":"xyz"}' http://localhost:8083/ -w "\n" {"bar":"xyz"}

Or you can bring your own JSON serializer like Chiron:https://github.com/xyncro/chiron

type A = { a : int } static member ToJson (x : A) = Json.writer "a" x.a


Json.format (Json.serialize { a = 42 }) val it = """{"a":42}"""

let app = GET >=> path "/test" >=> OK (UTF8.bytes it) >=> setMimeType "application/json; charset=utf-8"
namespace Suave
module Json

from Suave
namespace System
namespace System.Runtime
namespace System.Runtime.Serialization
type Foo =
  {foo: string;}

Full name: temp.Foo
Foo.foo: string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
type Bar =
  {bar: string;}

Full name: temp.Bar
Bar.bar: string
val startWebServer : config:SuaveConfig -> webpart:WebPart -> unit

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

Full name: Suave.Web.defaultConfig
val mapJson<'T1,'T2> : (('T1 -> 'T2) -> HttpContext -> Async<HttpContext option>)

Full name: Suave.Json.mapJson
val a : Foo
type A =
  {a: int;}
  static member ToJson : x:A -> 'a

Full name: temp.A
A.a: int
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
static member A.ToJson : x:A -> 'a

Full name: temp.A.ToJson
val x : A
val app : (obj -> Async<obj option>)

Full name: temp.app
union case HttpMethod.GET: HttpMethod
val path : pathAfterDomain:string -> WebPart

Full name: Suave.Filters.path
val OK : body:string -> WebPart

Full name: Suave.Successful.OK
module UTF8

from YoLo
val bytes : s:string -> byte []

Full name: YoLo.UTF8.bytes

results matching ""

    No results matching ""