The API to which I need to make request require parameters in specified order. At first I had used requests
lib
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.get("http://httpbin.org/get", params=payload)
Like is says in documents. requests
does not take parameters explicit
r = requests.get("http://httpbin.org/get", param1=payload1, param2=payload2)
After I had tryed urllib3 directly, but it returns me an error as above. From error:
def request_encode_body(self, method, url, fields=None, headers=None,
How to set http request parameters in specified order in Python, any libs.
Use a sequence of two-value tuples instead:
The reason
requests
doesn't retain ordering when using a dictionary is because python dictionaries do not have ordering. A tuple or a list, on the other hand, does, so the ordering can be retained.Demo: