Django Rest Framework web service - `Unsupported Media Type: /api/function_name`

I'm working on building a React (Next.js) application that will communicate with a DRF web service.

The application will not be interacting with a database very much but instead sending in data to crunch with the DRF app which will return derived values for display in the React app.

On a form submission, I have the following function run on the React side:

(values) => {
      fetch(http://localhost:8000/api/testing_post/", {
        mode: 'no-cors',
        method: "POST",
        headers: {'Content-Type': 'application/json'}, 
        body: JSON.stringify(values, null, 2)
      }).then(res => {
        console.log("Request complete! response:", res);
      });
    }

This posts the form data to the following view function in the DRF app:

@api_view(["POST"])
@parser_classes([JSONParser])
def testing_post(request):
    print(dir(request))
    print(request.data)
    return Response(status=status.HTTP_200_OK)

When I run without print(request.data), everything works as expected. However, when I include this line, I get a 415 error Unsupported Media Type: /api/testing_post/

I'm confused as to why I'm receiving this message - I'm clearly setting the content type in the request; and, on the server side, am using a JSON parser.

Does anyone know where the issue is stemming from? Thanks in advance!

Back to Top