httpcelerydjango-celerycelery-taskdjcelery

How to set http_headers on celery tasks calls via http


I want to make a task call via HTTP using the class HttpDispatch from celery, but i need to set Authorization header. How can i do this?

from celery.task.http import HttpDispatch
request = HttpDispatch(
     url='http://example.com/multiply',
     method='GET', {10})
request.dispatch()

Solution

  • You will need to subclass HttpDispatch and reimplement http_headers property method. This property is used inside HttpDispatch.

    class CustomHttpDispatch(HttpDispatch):
    
    @property
    def http_headers(self):
        headers = {
            'User-Agent': self.user_agent,
            'Authorization': 'XXX'}
    
        return headers