Url parameters

In addition to static string paths, we can specify route arguments. Suave comes with a cool feature called "typed routes", which gives you statically typed control over arguments for your route. As an example, let's see how we can add id of an album to the details route:

App.fs

  12: 
  13: 
pathScan "/store/details/%d" (fun id -> OK (sprintf "Details: %d" id))

This might look familiar to print formatting from C++, but it's more powerful. What happens here is that the compiler checks the type for the %d argument and complains if you pass it a value which is not an integer. The WebPart will apply for requests like $rootUrl/store/details/28. In the above example, there are a few important aspects:

  • sprintf "Details: %d" id is statically typed string formatting function, expecting the id as an integer
  • (fun id -> OK ...) is an anonymous function or lambda expression if you like, of type int -> WebPart
  • the lambda expression is passed as the second parameter to pathScan function
  • first argument of pathScan function also works as a statically typed format
  • type inference mechanism built into F# glues everything together, so that you do not have to mark any type signatures

To clear things up, here is another example of how one could use typed routes in Suave for request $rootUrl/store/details/abba/1:

App.fs

  14: 
  15: 
  16: 
pathScan "/store/details/%s/%d" (fun (a, id) -> OK (sprintf "Artist: %s; Id: %d" a id)) ]

We will not however keep that route in our application.

For more information on working with strings in a statically typed way, visit this site

namespace Suave
module Filters

from Suave
module Operators

from Suave
module Successful

from Suave
module Web

from Suave
val webPart : WebPart<HttpContext>

Full name: SuaveMusicStore.webPart
val choose : options:WebPart<'a> list -> WebPart<'a>

Full name: Suave.WebPart.choose
val path : pathAfterDomain:string -> WebPart

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

Full name: Suave.Successful.OK
val pathScan : pf:PrintfFormat<'a,'b,'c,'d,'t> -> h:('t -> WebPart) -> WebPart

Full name: Suave.Filters.pathScan
val id : int
val sprintf : format:Printf.StringFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
val a : string
val startWebServer : config:SuaveConfig -> webpart:WebPart -> unit

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

Full name: Suave.Web.defaultConfig

Show code from this section on GitHub

results matching ""

    No results matching ""