34

I need to create a secure restFUL api using sencha and django. I am fairly new to python. So far i am able to send request from sencha to server using basic authentication as below

 new Ext.data.Store({
   proxy: {
     type: "ajax",
      headers: {
       "Authorization": "Basic asdjksdfsksf="
    }
   }
 })  

In php/apache i can access those header with ease with the code below

$headers = apache_request_headers();
print_r($headers);

How to do this in python?

sumit
  • 15,003
  • 12
  • 69
  • 110

5 Answers5

57

You can access them within a view using request.META, which is a dictionary.

If you wanted the Authorization header, you could do request.META['HTTP_AUTHORIZATION']

If you're creating a restful API from scratch, you might want to take a look at using tastypie.

coffee-grinder
  • 26,940
  • 19
  • 56
  • 82
Jordan
  • 31,971
  • 6
  • 56
  • 67
  • yes i am using tastypie , is there anything more with these headers on tastypie – sumit May 16 '12 at 06:49
  • 1
    There's nothing in particular with tastypie and headers, but rather than rewriting your own authentication layer, you can use tastypie's built in one. http://django-tastypie.readthedocs.org/en/latest/authentication_authorization.html – Jordan May 16 '12 at 06:52
  • Thank you jordan , its request.META.get('Authorization') – sumit May 16 '12 at 07:22
  • Sorry about that. request.META['Authorization'] or request.META.get('Authorization') – Jordan May 16 '12 at 12:18
  • 6
    How does 'Authorization' work as a key? The docs say headers in META are upper-cased and have HTTP_ prepended. https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.META – Edward D'Souza Mar 11 '13 at 20:35
  • You're probably right Edward. I updated the answer to reflect that. – Jordan Mar 12 '13 at 16:15
13

As of django 2.2 HttpRequest.headers were added to allow simple access to a request’s headers. So now you can also get authentication header using get() function on request.headers

request.headers.get('Authorization')

This will give you value token value back.

Bearer eyJ0eYourToken...

https://docs.djangoproject.com/en/2.2/ref/request-response/#django.http.HttpRequest.headers

mxr7350
  • 1,438
  • 3
  • 21
  • 29
  • 2
    I recommend this because `request.META.get('HTTP_AUTHORIZATION') may return nothing if testing using the Django test client. – RaenonX Sep 08 '20 at 07:07
10

You can use

request.META['HTTP_AUTHORIZATION']

and sometimes

request.META['Authorization']

can help.

Juba Fourali
  • 740
  • 9
  • 10
1

For older versions of django prior to 2.2, you'll need to access the headers in the following way using the META key. Always important to first check if the key authorization header keys exists just in case it wasn't posted otherwise you'll run into non-existent key errors.

if not ('HTTP_AUTHORIZATION' in request.META.keys()):

    return HttpResponse('NO AUTH HEADER PROVIDED')

elif (request.META['HTTP_AUTHORIZATION'] == 'Bearer YourAuthorizationKey123':

    # Validation passed - Proceed with whatever else you want to do
Kevin Guto
  • 1,023
  • 6
  • 6
0

HttpRequest.headers

New in Django 2.2.

 if 'Authorization' in request.headers: # Authorization header exists
      #do something here
      pass
 
 else: # Authorization header not exists
     #do something here
     pass 

Read also Django official Doc