3
votes

I am programming a client interface to a restful web service in python and unfortunately the web service requires custom headers to be present in the request. I have been using Requests for this however the web service also requires the headers to be in a specific order in the request. I haven't been able to figure out how Requests orders the headers and see if there is a way to be able to control this ordering.

I am also open to using another module other than Requests in my application if someone has a recommendation.

2
If I were you I would file a bug report with the maintainers of the API as this is a clear violation of the HTTP spec. RFC 2616 Section 4.2 states: The order in which header fields with differing field names are received is not significant. Requiring a specific order for headers isn't just inconvenient, it's incorrect. - rdlowrey
Completely agree but unfortunately that will take some time and I am under pressure to complete this project. Thanks. - user2509689

2 Answers

4
votes

Updated answer

The answer below concerns versions below 2.9.2. Since version 2.9.2 (around April 2016) using an OrderedDict works again.

Old answer

It looks like it was possible some time ago using just the built-in functionality (issue 179). I think it is not anymore (issue 2057) and one of the reasons is mentioned in another comment by num1. I have used a following solution/workaround.

import requests
import collections

class SortedHTTPAdapter(requests.adapters.HTTPAdapter):
    def add_headers(self, request, **kwargs):
        request.headers = collections.OrderedDict(
            ((key, value) for key, value in sorted(request.headers.items()))
        )

session = requests.Session()
session.mount("http://", SortedHTTPAdapter())

In the example headers are just sorted but one can order them in any way. I chose that method after going through requests code and reading the method's docstring:

Add any headers needed by the connection. As of v2.0 this does nothing by default, but is left for overriding by users that subclass the HTTPAdapter <requests.adapters.HTTPAdapter> class.

For absolute control one could override the send method probably.

2
votes

You can try to use the OrderedDict class to store the headers, instead of request's default one:

>>> from collections import OrderedDict
>>> from requests import Session
>>> s = Session()
>>> s.headers
CaseInsensitiveDict({'Accept-Encoding': ... 'User-Agent': ... 'Accept': '*/*'})
>>> s.headers = OrderedDict([('User-Agent', 'foo-bar'), ('Accept', 'nothing')])
>>> s.headers
OrderedDict([('User-Agent', 'foo-bar'), ('Accept', 'nothing')])