Often times when working with web applications, parameters and values are passed in the path as well as the query string of a URL. In order to do something useful, parsing of the url is required. Noted below are examples of how this can be done in an elegant manner using Racket.
The first step is to import the appropriate packages
Assuming we are working with the url string “https://hacker-news.firebaseio.com/v0/item/192327.json?print=pretty“, we can convert the string to a URL struct using the
(define myurl (string->url "https://hacker-news.firebaseio.com/v0/item/192327.json?print=pretty"))
The result is the following output
;; (path/param "v0" '())
;; (path/param "item" '())
;; (path/param "192327.json" '()))
;; '((print . "pretty"))
The structure of the URL struct is as follows and directly maps to the values displayed above.
(struct url ( scheme
To access the individual data members of the struct, the syntax
<type>-<member> <instance>. What is returned can be further manipulated based on its type (string, list, boolean)