Introduction to URI module in elixir

Today, we will learn about the built-in elixir module for working with URIs.

This module provides functions for working with URIs (for example, parsing URIs or encoding query strings). The functions in this module are implemented according to RFC 3986.

Functions

char_reserved?(character)

Checks if character is a reserved one in a URI.

iex> URI.char_reserved?(?+)
      true

parse(uri)

parse(t() | binary()) :: t()

arses a well-formed URI reference into its components.

Note this function expects a well-formed URI and does not perform any validation. See the “Examples” section below for examples of how URI.parse/1 can be used to parse a wide range of URIs.

This function uses the parsing regular expression as defined in RFC 3986, Appendix B.

When a URI is given without a port, the value returned by URI.default_port/1 for the URI’s scheme is used for the :port field.

If a %URI{} the struct is given to this function, this function returns it unmodified.

Examples

iex> URI.parse("https://elixir-lang.org/")
      %URI{
        authority: "elixir-lang.org",
        fragment: nil,
        host: "elixir-lang.org",
        path: "/",
        port: 443,
        query: nil,
        scheme: "https",
        userinfo: nil
      }
iex> URI.parse("//elixir-lang.org/")
      %URI{
        authority: "elixir-lang.org",
        fragment: nil,
        host: "elixir-lang.org",
        path: "/",
        port: nil,
        query: nil,
        scheme: nil,
        userinfo: nil
      }
iex> URI.parse("/foo/bar")
      %URI{
        authority: nil,
        fragment: nil,
        host: nil,
        path: "/foo/bar",
        port: nil,
        query: nil,
        scheme: nil,
        userinfo: nil
      }
iex> URI.parse("foo/bar")
      %URI{
        authority: nil,
        fragment: nil,
        host: nil,
        path: "foo/bar",
        port: nil,
        query: nil,
        scheme: nil,
        userinfo: nil
      }

query_decoder(query)

query_decoder(binary()) :: Enumerable.t()
iex> URI.decode_query("foo=1&bar=2")
      %{"bar" => "2", "foo" => "1"}
iex> URI.decode_query("percent=oh+yes%21", %{"starting" => "map"})
      %{"percent" => "oh yes!", "starting" => "map"}

to_string(uri)

to_string(t()) :: binary()

Returns the string representation of the given URI struct.

iex> uri = URI.parse("http://google.com")
iex> URI.to_string(uri)
   "http://google.com"
iex> uri = URI.parse("foo://bar.baz")
iex> URI.to_string(uri)
   "foo://bar.baz"

Note that when creating this string representation, the :authority value will be used if the :host is nil. Otherwise, the :userinfo:host, and :port will be used.

iex> URI.to_string(%URI{authority: "foo@example.com:80"})
"//foo@example.com:80"
iex> URI.to_string(%URI{userinfo: "bar", host: "example.org", port: 81})
"//bar@example.org:81"
iex> URI.to_string(%URI{
 ...>   authority: "foo@example.com:80",
 ...>   userinfo: "bar",
 ...>   host: "example.org",
 ...>   port: 81
 ...> })
"//bar@example.org:81"

Leave a Reply

Your email address will not be published. Required fields are marked *