Python - Read headers inside controller?

3.5k views Asked by At

I'm building controller between source control system and Odoo in a way that specific integrated code source control system (like bitbucket, github) would be able to payload data using json. Reading of actual payloaded data is working, but what I'm struggling, is reading headers data inside controller.

I need headers data so I could identify from which system this payload is received (for example data structure might be different in bitbucket and github). Now if I would read that header, I would know which system payloads the data and how to parse it properly.

So my controller looks like this:

from odoo import http
from odoo.http import request


class GitData(http.Controller):
    """Controller responsible for receiving git data."""

    @http.route(['/web/git.data'], type='json', auth="public")
    def get_git_data(self, **kwargs):
        """Get git data."""
        # How to read headers inside here??
        data = request.jsonrequest
        # do something with data
        return '{"response": "OK"}'

Now for example I can call this route with:

import requests
import json

url = 'http://some_url/web/git.data'
headers = {
 'Accept': 'text/plain',
 'Content-Type': 'application/json',
 'type': 'bitbucket'}
data = {'some': 'thing'}

r = requests.post(url, data=json.dumps(data), headers=headers)

Now it looks that controller reads headers automatically, because it understands that it is json type. But what if I need to manually check specific header data like headers['type'] (in my example it was bitbucket)?

I tried looking into dir(self) and dir(request), but did not see anything related with headers. Also **kwargs is empty, so no headers there.

Note.: request object is actually:

# Thread local global request object
_request_stack = werkzeug.local.LocalStack()

request = _request_stack()
"""
    A global proxy that always redirect to the current request object.
""" 
# (This is taken from Odoo 10 source)

So basically it is part of werkzeug.

Maybe someone has more experience with werkzeug or controllers in general, so could point me in the right direction?

P.S. Also in Odoo itself I did not find any example that would read headers like I want. It looks the only place headers are used (actually setting them instead of reading), are after the fact, when building a response back.

1

There are 1 answers

2
Phillip Stack On BEST ANSWER
from openerp.http import request

Within your controller handling your specific path. You can access the request headers using the code below. (Confirmed Odoo8,Odoo10... probably works for Odoo9 as well)

headers = request.httprequest.headers