Python call URL with key=value pairs
Question: Since I do my first steps in Python, I try to figure out, how can I do a
simple URL call in Python with key=value pairs like:

    
    
    http://somehost/somecontroller/action?key1=value1&key2=value2
    

I tried with some things like:

    
    
    key1 = 'value'
    key2 = 10
    requests.get("http://somehost/somecontroller/action?key1=" + value + "&key2=" + str(10))
    

or

    
    
    data={'key1': 'value', 'key2': str(10)}
    requests.get("http://somehost/somecontroller/action", params=data)
    

(from [here](http://stackoverflow.com/questions/22974772/querystring-array-
parameters-in-python-using-requests))

But this don't work. I also tried it by calling `curl` with
`subprocess.Popen()` on different ways, but uhm...

I don't want to check the request, the URL call will be enough.


Answer: Using the `requests` library you can use
[`PreparedRequest.prepare_url`](http://docs.python-
requests.org/en/latest/api/#requests.PreparedRequest.prepare_url) to encode
the parameters to an url, like:

    
    
    import requests
    p = requests.models.PreparedRequest()
    
    data={'key1': 'value', 'key2': str(10)}
    p.prepare_url(url='http://somehost.com/somecontroller/action', params=data)
    
    print p.url
    

In the last print, you should get:

    
    
    http://somehost.com/somecontroller/action?key2=10&key1=value
    

