You misread the documentation. You need to do two things:

  1. Quote each key and value from your dictionary, and
  2. Encode those into a URL

Luckily urllib.parse.urlencode does both those things in a single step, and that's the function you should be using.

from urllib.parse import urlencode, quote_plus

payload = {'username':'administrator', 'password':'xyz'}
result = urlencode(payload, quote_via=quote_plus)
# 'password=xyz&username=administrator'
Answer from Adam Smith on Stack Overflow
🌐
Python
docs.python.org › 3 › library › urllib.parse.html
urllib.parse — Parse URLs into components — Python 3.14.6 ...
Use the urllib.parse.urlencode() function to convert such lists of pairs into query strings.
🌐
GitHub
gist.github.com › nkentaro › 37f25b802e825da7ab3b7f27c0303047
url encode and decode in python3 · GitHub
url encode and decode in python3. GitHub Gist: instantly share code, notes, and snippets.
🌐
Urlencoder
urlencoder.net › python-urlencode
Python urlencode() - URL Encode
>>> import urllib >>> params = urllib.urlencode( {'param': 'url encoder'} ) >>> print "http://example.com/?" + params
🌐
WebScraping.AI
webscraping.ai › faq › urllib3 › how-do-i-encode-query-parameters-with-urllib3
How do I encode query parameters with urllib3? | WebScraping.AI
import urllib3 from urllib.parse import urlencode # Initialize connection pool manager http = urllib3.PoolManager() # Define query parameters query_params = { 'search': 'web scraping', 'category': 'python tools', 'page': 1, 'active': True } # Encode parameters encoded_params = urlencode(query_params) print(f"Encoded: {encoded_params}") # Output: search=web+scraping&category=python+tools&page=1&active=True # Construct URL and make request url = f'https://api.example.com/search?{encoded_params}' response = http.request('GET', url) print(f"Status: {response.status}") print(f"Data: {response.data.decode('utf-8')}")
🌐
w3resource
w3resource.com › python-exercises › urllib3 › python-urllib3-exercise-11.php
Constructing URL: Proper Query Parameter Encoding
September 3, 2025 - # Import the urllib3 library import urllib3 # Define the base URL without query parameters base_url = 'https://example.com/api' # Define the query parameters as a dictionary query_params = { 'param1': 'value1', 'param2': 'value2', 'param3': 'special character: $', } # Use the urllib3 urlencode function to encode the query parameters encoded_params = urllib3.request.urlencode(query_params) # Combine the base URL and encoded query parameters to form the complete URL complete_url = f"{base_url}?{encoded_params}" # Print the complete URL print(f"Complete URL: {complete_url}")
🌐
iO Flood
ioflood.com › blog › python-url-encode
[SOLVED] Python URL Encode Using urllib.parse Functions
February 1, 2024 - In this example, we first define a dictionary params that contains our query parameters. When we pass this dictionary to the urlencode() function, it returns a new string where all special characters have been replaced with their corresponding percent-encoded ASCII characters.
🌐
Delft Stack
delftstack.com › home › howto › python › python urlencode
How to Use Urlencode in Python | Delft Stack
February 15, 2024 - In Python, we can URL encode a query string using the urlib.parse module, which further contains a function urlencode() for encoding the query string in the URL, and the query string is simply a string of key-value pairs.
Find elsewhere
🌐
MojoAuth
mojoauth.com › binary-encoding-decoding › percent-encoding-url-encoding-with-python
Percent-encoding (URL Encoding) with Python | Binary Encoding Techniques Across Programming Languages
from urllib.parse import urlencode params = { "query": "my search query", "filter": "category=electronics&brand=sony" } encoded_query_string = urlencode(params) print(encoded_query_string) # Output: query=my search query&filter=category=electronics&brand=sony
🌐
Linux Hint
linuxhint.com › urlencode-python
How to urlencode in Python? – Linux Hint
Execution of this file takes place at the terminal via the python3 query as below. The output result is showing the encoding of a string successfully. ... In the above example, you have seen that we have used the quote() function to encode or quote a string-type variable, and it worked perfectly. On the other hand, you need to understand that we cannot apply the “urlencode” method on any string because the string cannot be encoded into any URL.
🌐
URL Encoder
urlencoder.io › python
How to encode URLs in Python | URLEncoder
The urlencode() function takes an optional argument called doseq.
🌐
URLDecoder
urldecoder.io › python
URL Decoding query strings or form parameters in Python | URLDecoder
If you want to decode or parse multiple query strings of type application/x-www-form-urlencoded (e.g 'name=John+Doe&phone=+919999999999'), then you can use parse_qs or parse_qsl functions provided by urllib.parse package.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-urlencode-a-querystring-in-python
How to Urlencode a Querystring in Python? - GeeksforGeeks
July 23, 2025 - URL encoding a query string consists of converting characters into a format that can be safely transmitted over the internet. This process replaces special characters with a '%' followed by their hexadecimal equivalent.
🌐
Blogger
letconex.blogspot.com › 2020 › 03 › encode-and-decode-urls-in-python-3.html
Compiled blog: Encode and decode URLs in Python 3
March 3, 2020 - >>> urllib.parse.unquote('My+name+is+Rajeev') 'My+name+is+Rajeev' But if you’re working with HTML forms then you’ll need to replace plus sign (+) with space character because HTML forms use application/x-www-form-urlencoded MIME type which encodes space character to plus sign (+) instead of .
🌐
Devzery
devzery.com › post › urlencode-python-mastering-url-encoding-in-python
Python URL Encoding Made Simple: Learn urlencode, Query Strings & API Handling!
April 21, 2025 - The urlencode function is used to convert a dictionary or sequence of key-value pairs into a URL query string, ensuring that all characters are properly encoded.
🌐
URL Encode
urlencoder.org › enc › python
URL Encoding of "python" - Online
Encode python to URL-encoded format with various advanced options. Our site has an easy to use online tool to convert your data.
🌐
ProxiesAPI
proxiesapi.com › articles › url-encoding-and-decoding-in-python
URL Encoding and Decoding in Python | ProxiesAPI
February 6, 2024 - from urllib.parse import urlencode query_args = { "name": "John Doe" } encoded_args = urlencode(query_args) # Returns "name=John+Doe" with spaces encoded
🌐
DEV Community
dev.to › k4ml › python-urldecode-on-command-line-2ek9
Python urldecode on command line - DEV Community
December 2, 2018 - alias urldecode='python3 -c "import sys, urllib.parse as ul;print(ul.unquote_plus(sys.argv[1]))"'
🌐
GitHub
github.com › nvaccess › nvda › issues › 8717
In Python3 urllib nolonger contians urlopen urlencode · Issue #8717 · nvaccess/nvda
September 5, 2018 - Background: In python3 urllib.urlopen has been moved into urllib.request urlencode has been moved into urllib.parse There are also more changes in detail. These functions are used in updateCheck.py Possible solutions: from six.moves impo...
Author   nvaccess