Alex
Alex

Reputation: 531

Create query string without encoding

I want to create a query string but without encoding the special character like @, ! or ?.

Here's my code:

payload = {"key": "value", "key2": "value2",
           "email": "[email protected]", "password": "myPassword54321!?"}
print(urllib.parse.urlencode(payload))  

Now I get as output this:

password=myPassword54321%21%3F&email=test%40hello3.ch

How can I make my output look like this:

password=myPassword54321!?&[email protected]

Upvotes: 2

Views: 941

Answers (2)

L3viathan
L3viathan

Reputation: 27283

If you don't want to use the encoding features of urlencode, you may as well not use it, since it doesn't do that much else. If you just want to print the key-value pairs seperated by the & symbol, and each joined by an =, that is straightforward using str.join and str.format:

print("&".join("{}={}".format(key, value) for key, value in payload.items()))

Upvotes: 3

Patrick Haugh
Patrick Haugh

Reputation: 60974

urllib.parse.unquote will replace the %xx character escapes with the characters they represent

from urllib.parse import urlencode, unquote

print(unquote(urlencode(payload)))
# key=value&key2=value2&[email protected]&password=myPassword54321!?

Upvotes: 5

Related Questions