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
uri_type() :: nil | URL.Data.t() | URL.Geo.t() | URL.Tel.t() | URL.UUID.t() | URL.Mailto.t()
Link to this section 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.
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
}
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
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"}