I am implementing a client library for a private HTTP-API using python requests. The API(which I don't control) expects the parameters to be in a certain order, but python-requests doesn't honor a sorted dict as parameter.
This is what i tried:
import requests
from django.utils.datastructures import SortedDict
params = SortedDict()
params['s'] = 'value1'
params['f'] = 'value2'
requests.get('https://example.org/private_api', params=params)
#performs request as https://example.org/private_api?f=value1&s=value2
This is what I am trying to avoid:
requests.get('https://example.org?{0}'.format(urlencode(params)))
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: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