Why am I getting Unsupported media type "application/json" in request error?

Viewed 161

I'm using Postman to test an api. My local unit tests pass. But when I use the same request in Postman, I get

{
    "errors": [
        {
            "detail": "Unsupported media type \"application/json\" in request.",
            "status": "415",
            "source": {
                "pointer": "/data"
            },
            "code": "unsupported_media_type"
        }
    ]
}

Using a breakpoint, get that result when unpacking request.data in the View. I made sure to set application/json in the Content-Type header, and even specified it as a parser. I still get the error. Can you see why?

enter image description here enter image description here enter image description here

Part of the code where things break:

class ProvisionCustomerView(APIView):
    permission_classes = [IsAuthenticated]
    serializer_class = ProvisionCustomerSerializer

    def post(self, request):
        customer_data = request.data

When using postman, request.data returns:

*** rest_framework.exceptions.UnsupportedMediaType: Unsupported media type "application/json" in request.

Although it works fine in a unit test.

1 Answers

Turns out the reason was that I should've been using application/vnd.api+json in my Content-Type header. A team-member had reassigned a variable. Word to the wise: double check your settings file!

Related