URL v1.0.0 URL View Source

Functions for parsing URLs

This module provides functions for parsing URLs. It is modelled on Elixir’s URI module but will also parse scheme-specific URIs such as geo, data tel, mailto, and uuid.

Link to this section Summary

Functions

Parses a url and returns a %URL{} struct that has the same shape as Elixir’s %URI{} with the addition of the parsed_path key

Parses the given binary as parse_query

Parse a URL query string and percent decode

Link to this section Types

Link to this type t() View Source
t() :: %URL{
  authority: nil | binary(),
  fragment: nil | binary(),
  host: nil | binary(),
  parsed_path: uri_type(),
  path: nil | binary(),
  port: nil | :inet.port_number(),
  query: nil | binary(),
  scheme: nil | binary(),
  userinfo: nil | binary()
}

Link to this section Functions

Link to this function parse(url) View Source
parse(url :: binary()) :: URL.t()

Parses a url and returns a %URL{} struct that has the same shape as Elixir’s %URI{} with the addition of the parsed_path key.

Example

iex> URL.parse(“geo:48.198634,-16.371648,3.4;crs=wgs84;u=40.0”) %URL{

authority: nil,
fragment: nil,
host: nil,
parsed_path: %URL.Geo{
  alt: 3.4,
  lat: 48.198634,
  lng: -16.371648,
  params: %{"crs" => "wgs84", "u" => 40.0}
},
path: "48.198634,-16.371648,3.4;crs=wgs84;u=40.0",
port: nil,
query: nil,
scheme: "geo",
userinfo: nil

}

Link to this function parse_query(binary, opts \\ []) View Source
parse_query(binary(), keyword()) ::
  {:ok, [term()], rest, context, line, byte_offset}
  | {:error, reason, rest, context, line, byte_offset}
when line: {pos_integer(), byte_offset},
     byte_offset: pos_integer(),
     rest: binary(),
     reason: String.t(),
     context: map()

Parses the given binary as parse_query.

Returns {:ok, [token], rest, context, line, byte_offset} or {:error, reason, rest, context, line, byte_offset}.

Options

  • :line - the initial line, defaults to 1
  • :byte_offset - the initial byte offset, defaults to 0
  • :context - the initial context value. It will be converted to a map
Link to this function parse_query_string(query) View Source
parse_query_string(String.t() | uri_type()) ::
  Map.t() | {:error, {module(), binary()}}

Parse a URL query string and percent decode.

Returns

  • Either a map of query params or

  • an {:error, {URL.Parser.ParseError, reason}} tuple

Example

iex> URL.parse_query_string "url=http%3a%2f%2ffonzi.com%2f&name=Fonzi&mood=happy&coat=leather"
%{
  "coat" => "leather",
  "mood" => "happy",
  "name" => "Fonzi",
  "url" => "http://fonzi.com/"
}

iex> mailto = "mailto:user@%E7%B4%8D%E8%B1%86.example.org?subject=Test&body=NATTO"
iex> URL.parse(mailto) |> URL.parse_query_string
%{"body" => "NATTO", "subject" => "Test"}