1. 1. Source

Working with URLs in Racket

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

1
2
(require net/url)
(require net/url-structs)

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 string->url procedure.

1
(define myurl (string->url "https://hacker-news.firebaseio.com/v0/item/192327.json?print=pretty"))

The result is the following output

1
2
3
4
5
6
7
8
9
10
11
12
13
myurl
;;(url
;; "https"
;; #f
;; "hacker-news.firebaseio.com"
;; #f
;; #t
;; (list
;; (path/param "v0" '())
;; (path/param "item" '())
;; (path/param "192327.json" '()))
;; '((print . "pretty"))
;; #f)

The structure of the URL struct is as follows and directly maps to the values displayed above.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
(struct url ( scheme
user
host
port
path-absolute?
path
query
fragment)
#:extra-constructor-name make-url)
scheme : (or/c false/c string?)
user : (or/c false/c string?)
host : (or/c false/c string?)
port : (or/c false/c exact-nonnegative-integer?)
path-absolute? : boolean?
path : (listof path/param?)
query : (listof (cons/c symbol? (or/c false/c string?)))
fragment : (or/c false/c string?)

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)

1
2
(url-host myurl)
;;"hacker-news.firebaseio.com"
Source

URLS and HTTP