Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django http 403 error with Django 1.8 and Python 3.4 under macos 10.10.5

I try this routine, use curl to send a post to server, and want it to return http body.

Command:

curl --data "hi server" http://127.0.0.1:8000/verification/activate/

Server Side:

def activate(request):
    if request.method == 'POST':
        return HttpResponse(request.body)
    else:
        return HttpResponse('please send a post http.')

The string, "hi server", should be return message. but I got this error:

[18/Aug/2015 03:16:34]"POST /verification/activate/ HTTP/1.1" 403 2629
like image 512
attolee Avatar asked Dec 13 '25 17:12

attolee


1 Answers

The CSRF middleware is activated by default in the MIDDLEWARE_CLASSES setting. This middleware returns HTTP 403 in case the csrf token is missing in request body.

You can either remove django.middleware.csrf.CsrfViewMiddleware from MIDDLEWARE_CLASSES array in settings.py or use csrf_exempt() decorator to disable csrf protection just for a single view:

(Please note that the first option is strongly discouraged for security measures)

from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def activate(request):
    ...
like image 114
Ozgur Vatansever Avatar answered Dec 16 '25 14:12

Ozgur Vatansever



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!