Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom HTTP Header in Django

Tags:

django

I use a custom http header for URL signature just called "sign", how to get such custom HTTP header value in Django?

like image 345
Jason Xu Avatar asked Jan 17 '13 10:01

Jason Xu


People also ask

How do I create a custom header in Django?

Note in Django you write the header name in capitals with underscores instead of dashes, but in the request on the client you must write it using dashes instead of underscores (production web servers will strip out custom headers with underscores in them for security reasons).

What is HttpResponse in Django?

HttpResponse (source code) provides an inbound HTTP request to a Django web application with a text response. This class is most frequently used as a return object from a Django view.

How do I get the HTTP response header?

getKey() + " ,Value : " + entry. getValue()); } //get header by 'key' String server = conn. getHeaderField("Server");


2 Answers

Go ahead and use:

request.META.get('HTTP_{your uppercased header name}') 

Note in Django you write the header name in capitals with underscores instead of dashes, but in the request on the client you must write it using dashes instead of underscores (production web servers will strip out custom headers with underscores in them for security reasons).

So, a custom header My-Custom-Header is accessed request.META['HTTP_MY_CUSTOM_HEADER']

like image 193
owencm Avatar answered Sep 29 '22 18:09

owencm


Finally I found just get it through

request.META('HTTP_{your uppercased header name}') 
like image 27
Jason Xu Avatar answered Sep 29 '22 17:09

Jason Xu