不支持的媒体类型 Django API
Posted
技术标签:
【中文标题】不支持的媒体类型 Django API【英文标题】:Unsupported media type Django API 【发布时间】:2018-06-01 03:16:36 【问题描述】:我是 Django 的新手,我的项目是 REST 当我使用邮递员检查 URL 时,会出现此错误:
"detail": "不支持的媒体类型\"multipart/form-data;边界=----WebKitFormBoundaryAU1ShXHTHrfcQr61\"在请求中。"
当使用 curl 时发生了这个错误:
"detail":"JSON 解析错误 - 预期值:第 1 行第 1 列 (char 0)"
这是我对示例 URL 的看法:
@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)
【问题讨论】:
请发布处理请求的代码。仅根据错误消息无法猜测出什么问题。 @manassehkatz 我已经完成了 【参考方案1】:在邮递员设置中,body 为 Raw 并从 下拉列表 中选择 JSON 并发送请求。
或
更改 Django rest 框架中的解析器
settings.py
REST_FRAMEWORK =
'DEFAULT_PARSER_CLASSES': (
'rest_framework.parsers.FormParser',
'rest_framework.parsers.MultiPartParser'
)
【讨论】:
感谢您的提示!我派生自错误的基本解析器类! 我一直觉得这很痛苦。后端开发出于某种原因,您总是需要尝试“form-data”、“form-urlencoded”、“raw”等来查看哪个有效。而且现在不仅要原始,还要设置语言……【参考方案2】:您可能正在使用您的方法尚未准备好使用的 MIME 类型发送请求。 在此处查看媒体类型:https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Complete_list_of_MIME_types
没有看你的要求就不能说太多,但我希望它会有所帮助。
【讨论】:
以上是关于不支持的媒体类型 Django API的主要内容,如果未能解决你的问题,请参考以下文章
错误“不支持媒体类型‘application/json’”Api GMAIL