running man
running man

Reputation: 1467

Without params sorted descending when using python requests get method

I am using python requests. I do not want the params follow a descending order.

For example.

In  []:  import requests
         payload = {'key2': 'value1', 'key3': ['value', 'value3']}
         r = requests.get("http://httpbin.org/get", params=payload)
         print r.url

out []:  http://httpbin.org/get?key3=value&key3=value3&key2=value1

What I want is http://httpbin.org/get?key2=value1&key3=value&key3=value3.

Upvotes: 0

Views: 172

Answers (1)

wanderlust
wanderlust

Reputation: 1936

Despite of there is no difference in the params order, you can always use OrderedDict, as follows:

from collections import OrderedDict
pl = OrderedDict()
pl['key2'] = 'value1'
pl['key3'] = ['value', 'value3']
r = requests.get("http://httpbin.org/get", params=pl)

print r.url  # http://httpbin.org/get?key2=value1&key3=value&key3=value3

Please pay attention, you should add items to the orderedDict after its init, because constructor treats params during initialization as regular dict, that doesn't preserve keys order.

Upvotes: 1

Related Questions