I want to use query strings to pass values through a URL. For example:
http://127.0.0.1:5000/data?key=xxxx&secret=xxxx
In Python, how can I add the variables to a URL? For example:
key = "xxxx"
secret = "xxxx"
url = "http://127.0.0.1:5000/data?key=[WHAT_GOES_HERE]&secret=[WHAT_GOES_HERE]"
The safest way is to do the following:
import urllib
args = {"key": "xxxx", "secret": "yyyy"}
url = "http://127.0.0.1:5000/data?{}".format(urllib.urlencode(args))
You will want to make sure your values are url encoded.
The only characters that are safe to send non-encoded are [0-9a-zA-Z] and $-_.+!*'()
Everything else needs to be encoded.
For additional information read over page 2 of RFC1738
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With