51

I'm sending a post request to my API made using django rest framework:

curl --header "X-MyHeader: 123" --data "test=test" http://127.0.0.1:8000/api/update_log/

In my rest framework view, I want to get my costum header, and if the custom header satisfies a condition, I will proceed to analyze my post data.

Ok, my view looks like:

class PostUpdateLogView(APIView):
    throttle_classes = ()
    permission_classes = ()
    parser_classes = (
        parsers.FormParser,
        parsers.MultiPartParser,
        parsers.JSONParser,
    )  

    renderer_classes = (renderers.JSONRenderer,)

    def post(self, request):
        print request.Meta
        # Get custom header
        # Validate custom header
        # Proceed to analize post data

        # Make response
        content = {
            'response': 'response',
        }

        return Response(content)

I'm trying to find my custom header on request.Meta element, but when I print request.Meta, I get a 500 error. If I print request.data, I get the expected response.

¿What is the way to get a custom header on my post request using django rest framework?

3 Answers 3

90

The name of the meta data attribute of request is in upper case:

print request.META

If your header is called "My-Header", your header will be available as:

request.META['HTTP_MY_HEADER']

Or:

request.META.get('HTTP_MY_HEADER') # return `None` if no such header

Quote from the documentation:

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm sorry, like a missing (;), I was two hours on this... Maybe, do you know how can see the bug detail when I request the api using curl? when i send the post request using curl, i only can see on my console the answer like [05/Feb/2015 08:40:39] "POST /api/update_log/ HTTP/1.1" 500 78112, but I can't see the bug detail. Thank you and excuse my carelessness.
Redirect the output of the curl to the file and then open this file in the browser. curl ... http://127.0.0.1:8000/api/update_log/ > error.html
20

Seeing this is an old post, I thought I would share the newer and more flexible approach that is available since Django 2.2. You can use the request's headers object:

# curl --header "X-MyHeader: 123" --data "test=test" http://127.0.0.1:8000/api/update_log/

request.headers['X-MYHEADER']       # returns "123"
request.headers['x-myheader']       # case-insensitive, returns the same
request.headers.get('x-myheader')   # returns None if header doesn't exist

# standard headers are also available here
request.headers.get('Content-Type') # returns "application/x-www-form-urlencoded"

The biggest differences with request.META are that request.headers doesn't prepend headers with HTTP_, it doesn't transform the header names to UPPER_SNAKE_CASE and that the headers can be accessed case-insensitively. It will only transform the header to Title-Casing (e.g. X-Myheader) when displayed.

Comments

12

If you provide a valid header information and get that information from backend then follow those

client-name='ABCKD'

then you have get that client information in post or get function following this-

request.META['HTTP_CLIENT_NAME']

it will give you output 'ABCKD'.

remember that, whatever the valid variable name you provide in your header information in request, django convert it uppercase and prefix with 'HTTP_' in here it will client-name converted to CLIENT_NAME and prefix with HTTP_. so final output is HTTP_CLIENT_NAME

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.