URLs

The URL class handles URL validation and parsing.

httpx
>>> url = httpx.URL('https://www.example.com/')
>>> url
<URL 'https://www.example.com/'>

URL components are normalised, following the same rules as internet browsers.

httpx
>>> url = httpx.URL('https://www.EXAMPLE.com:443/path/../main')
>>> url
<URL 'https://www.example.com/main'>

Both absolute and relative URLs are valid.

httpx
>>> url = httpx.URL('/README.md')
>>> url
<URL '/README.md'>

Coercing a URL to a str will always result in a printable ASCII string.

httpx
>>> url = httpx.URL('https://example.com/path to here?search=🦋')
>>> str(url)
'https://example.com/path%20to%20here?search=%F0%9F%A6%8B'

URL components

The following properties are available for accessing the component parts of a URL.

A parsed representation of the query parameters is accessible with the .params property.

URLs can be instantiated from their components...

httpx
>>> httpx.URL(scheme="https", host="example.com", path="/")
<URL 'https://example.com/'>

Or using both the string form and query parameters...

httpx
>>> httpx.URL("https://example.com/", params={"search": "some text"})
<URL 'https://example.com/?search=some+text'>

Modifying URLs

Instances of URL are immutable, meaning their value cannot be changed. Instead new modified instances may be created.


Query Parameters

The QueryParams class provides an immutable multi-dict for accessing URL query parameters.

They can be instantiated from a dictionary.

httpx
>>> params = httpx.QueryParams({"color": "black", "size": "medium"})
>>> params
<QueryParams 'color=black&size=medium'>

Multiple values for a single key are valid.

httpx
>>> params = httpx.QueryParams({"filter": ["60GHz", "75GHz", "100GHz"]})
>>> params
<QueryParams 'filter=60GHz&filter=75GHz&filter=100GHz'>

They can also be instantiated directly from a query string.

httpx
>>> params = httpx.QueryParams("color=black&size=medium")
>>> params
<QueryParams 'color=black&size=medium'>

Keys and values are always represented as strings.

httpx
>>> params = httpx.QueryParams("sort_by=published&author=natalie")
>>> params["sort_by"]
'published'

When coercing query parameters to strings you'll see the same escaping behavior as HTML form submissions. The result will always be a printable ASCII string.

httpx
>>> params = httpx.QueryParams({"email": "user@example.com", "search": "How HTTP works!"})
>>> str(params)
'email=user%40example.com&search=How+HTTP+works%21'

Accessing query parameters

Query parameters are accessed using a standard dictionary style interface...

The following methods are also available for accessing query parameters as a multidict...

Modifying query parameters

The following methods can be used to create modified query parameter instances...


Responses Headers