HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulehttp-types-0.12.4Haskell2010

Network.HTTP.Types.URI

Query strings generally have the following form: "key1=value1&key2=value2"

Example1 expression
renderQuery False [("key1", Just "value1"), ("key2", Just "value2")]"key1=value1&key2=value2"

But if the value of key1 is Nothing, it becomes: key1&key2=value2

Example1 expression
renderQuery False [("key1", Nothing), ("key2", Just "value2")]"key1&key2=value2"

This module also provides type synonyms and functions to handle queries that do not allow/expect keys without values (SimpleQuery), handle queries which have partially escaped characters

  • 8 types
  • 22 values

Query strings

0 declarations

Query

valueparseQuery :: ByteString -> Query
#

Split out the query string into a list of keys and values. A few importants points:

  • The result returned is still bytestrings, since we perform no character decoding here. Most likely, you will want to use UTF-8 decoding, but this is left to the user of the library.

  • Percent decoding errors are ignored. In particular, "%Q" will be output as "%Q".

  • It decodes '+' characters to ' '

Query (Text)

SimpleQuery

If values are guaranteed, it might be easier working with SimpleQuery.

This way, you don't have to worry about any Maybes, though when parsing a query string and there's no '=' after the key in the query item, the value will just be an empty ByteString.

PartialEscapeQuery

For some values in query items, certain characters must not be percent-encoded, for example '+' or ':' in

q=a+language:haskell+created:2009-01-01..2009-02-01&sort=stars

Using specific EscapeItems provides a way to decide which parts of a query string value will be URL encoded and which won't.

This is mandatory when searching for '+' (%2B being a percent-encoded '+'):

q=%2B+language:haskell
datadata EscapeItem
#

Section of a query item value that decides whether to use regular URL encoding (using 'urlEncode True') with QE, or to not encode anything with QN.

Constructors

Instances3Eq, Ord, Show
  • Eq EscapeItemDefined in http-types-0.12.4 · Network.HTTP.Types.URI
  • Ord EscapeItemDefined in http-types-0.12.4 · Network.HTTP.Types.URI
  • Show EscapeItemDefined in http-types-0.12.4 · Network.HTTP.Types.URI

Path

0 declarations

Segments + Query String

Extract whole path (path segments + query) from a RFC 2616 Request-URI.

Though a more accurate description of this function's behaviour is that it removes the domain/origin if the string starts with an HTTP protocol. (i.e. http:// or https://)

This function will not change anything when given any other ByteString. (except return a root path "/" if given an empty string)

Example1 expression
extractPath "/path""/path"
Example1 expression
extractPath "http://example.com:8080/path""/path"
Example1 expression
extractPath "http://example.com""/"
Example1 expression
extractPath """/"
Example1 expression
extractPath "www.google.com/some/path""www.google.com/some/path"

Path Segments

valueencodePathSegments :: [Text] -> Builder
#

Encodes a list of path segments into a valid URL fragment.

This function takes the following three steps:

  • UTF-8 encodes the characters.

  • Prepends each segment with a slash.

  • Performs percent-encoding on all characters that are not:

    • alphanumeric (i.e. A-Z and a-z)

    • digits (i.e. 0-9)

    • a dash '-', an underscore '_', a dot '.', or a tilde '~'

For example:

Example1 expression
encodePathSegments ["foo", "bar1", "~baz"]"/foo/bar1/~baz"
Example1 expression
encodePathSegments ["foo bar", "baz/bin"]"/foo%20bar/baz%2Fbin"
Example1 expression
encodePathSegments ["שלום"]"/%D7%A9%D7%9C%D7%95%D7%9D"

Huge thanks to Jeremy Shaw who created the original implementation of this function in web-routes and did such thorough research to determine all correct escaping procedures.

URL encoding / decoding

3 declarations

Percent-encoding for URLs.

In short:

  • if you're encoding (parts of) a path element, use False.

  • if you're encoding (parts of) a query string, use True.

In-depth explanation

This will substitute every byte with its percent-encoded equivalent unless:

  • The byte is alphanumeric. (i.e. A-Z, a-z, or 0-9)

  • The byte is either a dash '-', an underscore '_', a dot '.', or a tilde '~'

  • If False is used, the following will also not be percent-encoded:

    • colon ':', at sign '@', ampersand '&', equals sign '=', plus sign '+', dollar sign '$' or a comma ','