PUT请求图像上传不工作在django休息 [英] PUT request for image upload not working in django rest

查看:795
本文介绍了PUT请求图像上传不工作在django休息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 PUT 请求中的 multipart / form-data 在django rest中上传图像,枕头:

I am trying to upload an image in django rest using multipart/form-data in a PUT request and Pillow:

class ABC(APIView):
    parser_classes = (MultiPartParser,)
    def put(self, request):
        a = Image()
        a.image_url = request.data["image"]
        a.save()

class Image(models.Model):
      image_url = models.ImageField(upload_to='static/bills', blank=True)


$ b $我发出一个PUT请求和一个multipart / form-data的请求。我最终得到一个响应代码为400的消息:

I make a request which is a PUT request and a multipart/form-data. I end up getting a response code of 400 with the message:

{
  "detail": "Multipart form parse error - Invalid boundary in multipart: None"
}

不知何故,这已经破了。当我第一次写的时候,它的工作正常。从那时起,我为CORS请求添加了几个设置配置,如:

Somehow this has broken just now. It was working fine when I wrote it the first time. Since then I have added few settings configuration for CORS requests like:

CORS_ORIGIN_ALLOW_ALL = True

CORS_ALLOW_HEADERS = (
    'x-requested-with',
    'content-type',
    'accept',
    'origin',
    'authorization',
    'x-csrftoken',
    'token',
    'x-device-id',
    'x-device-type',
    'x-push-id',
    'dataserviceversion',
    'maxdataserviceversion'
)
CORS_ALLOW_METHODS = (
        'GET',
        'POST',
        'PUT',
        'PATCH',
        'DELETE',
        'OPTIONS'
    )

任何想法?

选项请求回复:

Access-Control-Allow-Headers → x-requested-with, content-type, accept, origin, authorization, x-csrftoken, token, x-device-id, x-device-type, x-push-id, dataserviceversion, maxdataserviceversion
    Access-Control-Allow-Methods → GET, POST, PUT, PATCH, DELETE, OPTIONS
    Access-Control-Allow-Origin → *
    Access-Control-Max-Age → 86400
    Allow → GET, POST, DELETE, HEAD, OPTIONS
    Content-Type → application/json
    Date → Fri, 21 Aug 2015 06:23:28 GMT
    Server → WSGIServer/0.1 Python/2.7.6
    Vary → Accept
    X-Frame-Options → SAMEORIGIN


推荐答案

从标头中删除内容类型可以解决这个问题。

Removing the content-type from the headers resolves this.

这篇关于PUT请求图像上传不工作在django休息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆