为什么我在请求中收到不支持的媒体类型“application/json”错误?

问题描述 投票:0回答:2

我正在使用 Postman 来测试 api。我的本地单元测试通过了。但是当我在 Postman 中使用相同的请求时,我得到了

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

使用断点,在视图中解压

request.data
时获得结果。 我确保在 Content-Type 标头中设置
application/json
,甚至将其指定为解析器。我仍然收到错误。你能明白为什么吗?

出现问题的部分代码:

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

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

使用postman时,request.data返回:

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

尽管它在单元测试中运行良好。

django django-rest-framework django-views postman
2个回答
0
投票

事实证明,原因是我应该在我的

application/vnd.api+json
标题中使用
Content-Type
。一名团队成员重新分配了一个变量。明智之举:仔细检查您的设置文件!


0
投票

2024 年 4 月,Tidal 预期的

Content-Type
现在是
application/vnd.tidal.v1+json

© www.soinside.com 2019 - 2024. All rights reserved.