Saturday 15 March 2014

Python HTTP request with controlled ordering of HTTP headers -


I am programming a client interface for a cool web service in Python and unfortunately the need for custom headers in the web service Is the request I am using requests for this but the web service must keep the header in a specific order in the request. I can not understand how to order orders and to see if there is any way to control this order.

I am also open to using other modules in addition to the requests in my application if anyone is recommended.

It seems that just underlying functionality some time ago () I think it is no longer () And one of the reasons has been mentioned in another comment. I have used the following solution / operation. Import import of request import class classified HTTP adapter (request.adapters.HTTPAdapter): def add_headers (self, request, ** kwargs): request. Headers = collections.OrderedDict (key, value), value in order (request.headers.items ())) session = requests.session () session.mount ("http: //", sorted HTTP adapter ())

Examples are just sorted in the header but no one can order them in any way. I have chosen that method to go through the request code and after reading the method's docstrings:

Add any headers required by the connection. As V2.0 does not do anything by default, but users are left to override which HTTPAdapter & lt; Request.adapters.HTTPAdapter & gt; class For absolute control, perhaps the send can override the method.

No comments:

Post a Comment