不支持的媒体类型Django API

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

我是Django的新手,我的项目在REST中,当我使用邮递员检查URL时,它会出现此错误:

“detail”:“不支持的媒体类型”multipart / form-data; boundary = ---- WebKitFormBoundaryAU1ShXHTHrfcQr61 \“请求中。”

当使用curl时发生此错误:

{“detail”:“JSON解析错误 - 期望值:第1行第1列(char 0)”}

这是我对示例网址的看法:

@api_view(["POST"])
def register(request):
    user_data = request.data
    if user_data:
        serializer = UserSerializers(data=user_data)

        if serializer.is_valid():

            phone_number = serializer.validated_data["phone"]

            try:
                found_user = Users.objects.get(phone=phone_number)
            except Users.DoesNotExist:
                found_user = None

            if found_user:
                return Response({
                    "code": 211,
                    "status": "successfull",
                    "message": "user already exists, try to login"
                })
            else:
                destination = serializer.validated_data['phone']
                message = str(random.randint(1000, 9999))
                url = "https://panel.asanak.ir/webservice/v1rest/sendsms/?Username=0216463&Password=123456&Source=02100064636463&Destination={}&message={}"
                url = url.format(destination, message)
                r = requests.get(url)
                r.json()
                registerInfo = {
                    'name': serializer.validated_data['name'],
                    'lastName': serializer.validated_data['lastName'],
                    'phone': serializer.validated_data['phone'],
                    'natNum': serializer.validated_data['natNum'],
                    'password': serializer.validated_data['password'],
                    'degImage': serializer.validated_data['degImage'],
                    'natImage': serializer.validated_data['natImage'],
                    'sex': "",
                    'province': "",
                    'city': "",
                    'job': "",
                    'code': message
                }

                serializer.save(registerInfo)

                return Response({
                    "code": 200,
                    "status": "successfull",
                    "message": "code was sent try to validate code"
                })
            return Response({
                "status": "successfull",
                "error": "user created :)"
            })

        else:
            return Response({
                "status": "error",
                "error": "Bad request"
            })
    else:
        return Response({
            "status": "error",
            "error": "Empty request"
        })

这是序列化器类:

class UserSerializers(serializers.Serializer):
    name = serializers.CharField(required=True)
    lastName = serializers.CharField(required=True)
    phone = serializers.CharField(required=True)
    natNum = serializers.IntegerField(required=True)
    password = serializers.CharField(required=True)
    degImage = serializers.ImageField(required=True)
    natImage = serializers.ImageField(required=True)

    def save(self, validated_data):
        user = Users(
            name=validated_data['name'],
            lastName=validated_data['lastName'],
            phone=validated_data['phone'],
            natNum=validated_data['natNum'],
            password=validated_data['password'],
            degImage=validated_data['degImage'],
            natImage=validated_data['natImage']
        )
        user.save()
        return user

和模型:

class Users(models.Model):
    name = models.CharField(max_length=20, null=True)
    lastName = models.CharField(max_length=50, null=True)
    phone = models.IntegerField(unique=True, null=False, default='phone')
    password = models.CharField(max_length=25, null=True)
    natNum = models.IntegerField(unique=True, null=True)
    degImage = models.ImageField(null=False, blank=False, default='detImage')
    natImage = models.ImageField(null=False, blank=False, default='natImage')
    sex = models.CharField(null=True, max_length=1)
    province = models.CharField(null=True, max_length=20)
    city = models.CharField(null=True, max_length=40)
    job = models.CharField(null=True, max_length=20)
    code = models.CharField(max_length=4, null=True)
python django rest django-rest-framework postman
2个回答
1
投票

您可能正在使用您的方法尚未准备好使用的MIME类型发送您的请求。 在这里查看媒体类型: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Complete_list_of_MIME_types

不看你的要求不能说太多,但我希望它有所帮助。


2
投票

在postman set中,body作为Raw并从下拉列表中选择JSON并发送请求。

要么

在Django休息框架中更改解析器

settings.朋友

 REST_FRAMEWORK = {
    'DEFAULT_PARSER_CLASSES': (
        'rest_framework.parsers.FormParser',
        'rest_framework.parsers.MultiPartParser'
     )
 }
© www.soinside.com 2019 - 2024. All rights reserved.