Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get data from Django Headers?

I'm facing problem to get data from Django Headers.

My API using CURL:-

curl -X POST \
  https://xyx.com \
  -H 'Cache-Control: no-cache' \
  -H 'Content-Type: application/json' \
  -H 'xyzId: 3223' \
  -H 'abcData: ABC-123' \
  -d '{
  "name": "xyz",
  "dob": "xyz",
  "user_info": "xyz",
}'

In my API I need to get xyzId and abcData

I tried request.META['abcData'] but got error KeyError.

How do I get Both data in my view?

Please help me to out this problem.

Thanks in advance.

like image 656
Mr Singh Avatar asked May 29 '18 12:05

Mr Singh


1 Answers

As per documentations say https://docs.djangoproject.com/en/2.0/ref/request-response/#django.http.HttpRequest.META

With the exception of CONTENT_LENGTH and CONTENT_TYPE, as given above, any HTTP headers in the request are converted to META keys by converting all characters to uppercase, replacing any hyphens with underscores and adding an HTTP_ prefix to the name. So, for example, a header called X-Bender would be mapped to the META key HTTP_X_BENDER.

So you should be able to access your header like this

request.META['HTTP_ABCDATA']
like image 152
Sardorbek Imomaliev Avatar answered Oct 17 '22 00:10

Sardorbek Imomaliev