Lo-Fi Python

Oct 25, 2023

Formatting URL Parameters in Python

When I first started working with APIs, I had a bad habit of passing URL parameters as one long ugly string. Anything longer than 79 characters violates PEP-8. It's also hard to read and can be difficult to edit the code in your text editor if the URL is trailing off the screen. In this post, you'll find some alternatives to the primitive "long ugly string" approach.

Did you know? URL stands for "uniform resource locator".

Below are two ways to neatly format your URLs so that they have parameters. Both involve using a Python dictionary. The requests API allows you to pass a dictionary or list of tuples to its params argument. Alternatively, if you want to see the full URL as a string, there's a sleek way to format URL arguments with urllib's urlencode function.

a visual breakdown of a url with parameters

source: Geeks for Geeks

Pass a dictionary to the requests params argument to include URL arguments.

You often want to send some sort of data in the URL’s query string. If you were constructing the URL by hand, this data would be given as key/value pairs in the URL after a question mark, e.g. httpbin.org/get?key=val. Requests allows you to provide these arguments as a dictionary of strings, using the params keyword argument. - requests documentation, Passing Parameters in URLs
1
2
3
4
5
6
7
8
9
import requests

payload = {
    "email": "[email protected]",
    "message": "This email is not real.",
    "status": "inactive"
}
r = requests.get("https://httpbin.org/get", params=payload)
print(r.text)

Use urllib's urlencode function to dynamically construct URL from a dictionary.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import requests
from urllib.parse import urlencode

payload = {
    "email": "[email protected]",
    "message": "This email is not real.",
    "status": "inactive"
}
# Returns str of URL encoded parameters.
url_parameters = urlencode(payload)
# >>> url_parameters
# "email=example%40example.com&message=This+email+is+not+real.&status=inactive"
url = f"https://httpbin.org/get?{url_parameters}"
r = requests.get(url)
print(r.text)

Arguments can be a good thing.

This seemingly basic HTTP formatting was something that took me too long to realize. I hope it helps you keep your URLs tidy and your HTTP requests more readable.

Read More About URL Parameters

Passing Parameters in URLS, requests Documentation

urllib Examples, Python Documentation

requests API Documentation Reference

Stack Overflow, Python Dictionary to URL Parameters