[Answered ]-Why am I getting 403 for POST with DRF and rest_framework_api_key?

1👍

From the Django REST Framework API Key docs:

By default, clients must pass their API key via the Authorization header. It must be formatted as follows:

Authorization: Api-Key <API_KEY>

You’re missing the Api-Key part (notice the space between it and the <API_KEY> part). So your request should be:

fetch('/create-speech', {
    ...
    headers: {
        'Authorization': 'Api-Key 0tSMNivu.f8DOBrHTTKfMQBGANNbjl5BJQcswN9ay',
        'Content-Type': 'application/json'
    }
})
...

0👍

You should add a CSRF exemption permission class or decorator, e.g.:

from django.views.decorators.csrf import csrf_exempt

and above your API view declaration:

@api_view(["POST"])
@csrf_exempt
def tts_view(request):

With this, ensuring that you pass in your DRF API token in headers in your API call as
Authorization: Token token_string
should suffice.

Leave a comment