Url Parsing in F#
open System
open System.Net
// We'll parse this example URL, which includes a
// scheme, authentication info, host, port, path,
// query params, and query fragment.
let s = "postgres://user:pass@host.com:5432/path?k=v#f"
// Parse the URL and ensure there are no errors.
let u = Uri(s)
// Accessing the scheme is straightforward.
printfn "%s" u.Scheme
// UserInfo contains all authentication info; we can extract
// username and password from it.
let userInfo = u.UserInfo.Split(':')
printfn "%s" u.UserInfo
printfn "%s" userInfo.[0]
printfn "%s" userInfo.[1]
// The Host contains both the hostname and the port,
// if present. Use Uri.Host and Uri.Port to extract them.
printfn "%s" u.Authority
printfn "%s" u.Host
printfn "%d" u.Port
// Here we extract the path and the fragment after
// the #.
printfn "%s" u.AbsolutePath
printfn "%s" u.Fragment
// To get query params in a string of k=v format,
// use Query. You can also parse query params
// into a map using HttpUtility.ParseQueryString.
printfn "%s" u.Query
let query = HttpUtility.ParseQueryString(u.Query)
printfn "%A" query
printfn "%s" (query.["k"])Running our URL parsing program shows all the different pieces that we extracted:
postgres
user:pass
user
pass
host.com:5432
host.com
5432
/path
#f
?k=v
System.Collections.Specialized.NameValueCollection
vIn this F# version:
We use the
System.Uriclass to parse the URL, which provides similar functionality to Go’surl.Parse.The
UserInfoproperty gives us the username and password together, which we split manually.Instead of
SplitHostPort, we use theHostandPortproperties of theUriclass.The
Queryproperty gives us the raw query string, and we useHttpUtility.ParseQueryStringto parse it into aNameValueCollection.F# uses
printfnfor formatted printing, similar to Go’sfmt.Println.Error handling is not explicitly shown here, but you could wrap the
Uricreation in a try-catch block if needed.
This example demonstrates how to parse and extract information from URLs in F#, covering similar concepts to the original Go example.