The requests lib now supports this out-of-the-box: To get ordered parameters you use a sequence of two-valued tuples instead. This eliminates the additional requirement of OrderedDict.
payload = (('key1', 'value1'), ('key2', 'value2'))
r = requests.get("http://httpbin.org/get", params=payload)
Demo:
>>> import requests
>>> requests.__version__
1.2.3
>>> payload = (('key1', 'value1'), ('key2', 'value2'), ('key3', 'value3'))
>>> r = requests.get("http://httpbin.org/get", params=payload)
>>> print r.json()['url']
http://httpbin.org/get?key1=value1&key2=value2&key3=value3
Answer from Jeff Sheffield on Stack OverflowThe requests lib now supports this out-of-the-box: To get ordered parameters you use a sequence of two-valued tuples instead. This eliminates the additional requirement of OrderedDict.
payload = (('key1', 'value1'), ('key2', 'value2'))
r = requests.get("http://httpbin.org/get", params=payload)
Demo:
>>> import requests
>>> requests.__version__
1.2.3
>>> payload = (('key1', 'value1'), ('key2', 'value2'), ('key3', 'value3'))
>>> r = requests.get("http://httpbin.org/get", params=payload)
>>> print r.json()['url']
http://httpbin.org/get?key1=value1&key2=value2&key3=value3
Currently requests doesn't allow to do this as you wish. This is of course shortcoming that will be fixed. However as params parameter can take not only dictionary but bytes as well you should be able to do something in between:
from collections import OrderedDict
from urllib import urlencode
import requests
params = OrderedDict([('first', 1), ('second', 2), ('third', 3)])
requests.get('https://example.org/private_api', params=urlencode(params))
This doesn't work as I see due to bug in line 85 of models.py: self.params = dict(params or [].
I raised this problem in issue Wrong handling of params given as bytes object
Using method GET with python requests
requests force override query string encoding
APIs: Params= vs json= whats the difference?
Print value from key using from a requests.get()-function
Videos
Been learning more about accessing APIs and stuff. I keep running into things about params and json to do requests. I don't exactly understand what the difference between these two is. I use the same dictionaries but the keyword differs.
I tried to read the documentation but its a bit more technical than my ability.