[Answered ]-Sending POST request with JSON data in DJANGO and response from view also JSON data but its giving 403 FORBIDDEN error

1👍

In addition to @ArpitGoyal’s answer you can also decorate your view with csrf_exempt:

This decorator marks a view as being exempt from the protection ensured by the middleware.

A few tips in case you do need CSRF protection:

  1. Check CSRF token cookie name.

    See CSRF_COOKIE_NAME for more information.

  2. Add ensure_csrf_cookie decorator to your view.

    According to the docs:

    Warning

    If your view is not rendering a template containing the csrf_token template tag, Django might not set the CSRF token cookie. This is common in cases where forms are dynamically added to the page. To address this case, Django provides a view decorator which forces setting of the cookie: ensure_csrf_cookie().

  3. Assuming that CSRF token cookie name is csrftoken, try to send X-CSRFToken header.

    $.ajax({
        // Your options here.
        headers: {'X-CSRFToken': getCookie('csrftoken')}
    });
    
👤Ernest

1👍

You should authenticate your client before making the request. From your call you are providing a ajax POST request hit.

Provide a header in your RESTClient: X-CSRFToken.

For more details view this

Leave a comment