Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

requests python lib adding in Accept header

so I have made a request to a server with pythons request library. The code looks like this (it uses an adapter so it needs to match a certain 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 am parsing in is this

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

however I am getting an xml response back from the server. After checking fiddler I see the request being sent is:

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 requests library is adding in this Accept:* / * header which is throwing off the server. Does anyone know how I can stop this?

like image 430
zidsal Avatar asked Jun 25 '13 09:06

zidsal


1 Answers

As stated in comments it seems this is a problem with the requests library in 3.3. In requests there are default headers (which can be found in the utils folder). When you don't specify your own headers these default headers are used. However if you specify your own headers instead requests tries to merge the headers together to make sure you have all the headers you need.

The problem shows its self in def request() method in sessions.py. Instead of merging all the headers it puts in its headers and then chucks in yours. For now I have just done the dirty hack of removing the accept header from the default headers found in util

like image 86
zidsal Avatar answered Oct 05 '22 07:10

zidsal