Title here
Summary here
Our URL parsing program demonstrates how to parse and extract information from URLs in Crystal.
require "uri"
require "socket"
# We'll parse this example URL, which includes a
# scheme, authentication info, host, port, path,
# query params, and query fragment.
s = "postgres://user:pass@host.com:5432/path?k=v#f"
# Parse the URL and ensure there are no errors.
u = URI.parse(s)
# Accessing the scheme is straightforward.
puts u.scheme
# `userinfo` contains all authentication info; we need to
# parse it manually to get individual username and password.
if userinfo = u.userinfo
puts userinfo
username, password = userinfo.split(":")
puts username
puts password
end
# The `host` and `port` are separate in Crystal's URI.
puts "#{u.host}:#{u.port}"
puts u.host
puts u.port
# Here we extract the `path` and the fragment after
# the `#`.
puts u.path
puts u.fragment
# To get query params in a string of `k=v` format,
# use `query`. You can also parse query params
# into a hash. The parsed query param hash is from
# strings to arrays of strings, so index into `[0]`
# if you only want the first value.
puts u.query
if query = u.query
m = URI::Params.parse(query)
puts m
puts m["k"][0] if m["k"]?
end
Running our URL parsing program shows all the different pieces that we extracted.
$ crystal run url_parsing.cr
postgres
user:pass
user
pass
host.com:5432
host.com
5432
/path
f
k=v
{"k" => ["v"]}
v
In this Crystal version:
URI
module to parse URLs, which is part of Crystal’s standard library.URI.parse
method is used instead of url.Parse
.URI
struct provides properties like scheme
, host
, port
, path
, fragment
, and query
directly.host
and port
are separate properties in Crystal’s URI
.URI::Params.parse
instead of url.ParseQuery
.This example demonstrates how to work with URLs in Crystal, which provides a robust set of tools for URL parsing and manipulation.