Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

accessing request headers on django/python

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?

like image 889
sumit Avatar asked May 16 '12 06:05

sumit


People also ask

How do I get all the headers in Python?

To get all headers, you can use request. headers. keys() or request. headers.

How do I see requests in Django?

It's possible that a request can come in via POST with an empty POST dictionary -- if, say, a form is requested via the POST HTTP method but does not include form data. Therefore, you shouldn't use if request. POST to check for use of the POST method; instead, use if request. method == "POST" (see above).


2 Answers

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.

like image 197
Jordan Avatar answered Sep 28 '22 11:09

Jordan


You can use

request.META['HTTP_AUTHORIZATION'] 

and sometimes

request.META['Authorization'] 

can help.

like image 25
Juba Fourali Avatar answered Sep 28 '22 12:09

Juba Fourali