Requires adding python lib in Accept header

so I made a request to the server with the pythons request library. The code looks like this (it uses an adapter to match a specific pattern)

def getRequest(self, url, header): """ implementation of a get request """ conn = requests.get(url, headers=header) newBody = conn.content newHeader = conn.headers newHeader['status'] = conn.status_code response = {"Headers" : newHeader, "Body" : newBody.decode('utf-8')} self._huddleErrors.handleResponseError(response) return response 

the header parameter I'm processing is

 {'Authorization': 'OAuth2 handsOffMyToken', 'Accept': 'application/vnd.huddle.data+json'} 

however i get xml response from server. After checking the violinist, I see that the sent request:

 Accept-Encoding: identity Accept: */* Host: api.huddle.dev Authorization: OAuth2 HandsOffMyToken Accept: application/vnd.huddle.data+json Accept-Encoding: gzip, deflate, compress User-Agent: python-requests/1.2.3 CPython/3.3.2 Windows/2008ServerR2 

As we can see, there are 2 Accept Headers! The query library adds Accept: * / * to this header, which the server drops. Does anyone know how I can stop this?

+4
source share
1 answer

As pointed out in the comments, this seems to be a problem with the query library in 3.3. Requests have default headers (which can be found in the utils folder). If you do not specify your own headers, these headers are used by default. However, if you specify your own headers, the queries will instead try to combine the headers together to make sure that you have all the necessary headers.

The problem shows its self in the def request () method in session.py. Instead of merging all the headers, it is placed in its headers and then gets stuck in yours. At the moment, I just made a dirty hack of removing the accept header from the default headers found in the utility

+1
source

Source: https://habr.com/ru/post/1488032/


All Articles