PUT请求图片上传无法在django休息

时间:2021-12-25 03:47:14

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

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

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)

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:

我发出了一个请求,它是一个PUT请求和一个multipart / form-data。我最终得到的响应代码为400,并带有以下消息:

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

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请求添加了一些设置配置,例如:

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'
    )

Any ideas?

有任何想法吗?

OPTIONS Request response:

选项请求响应:

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

3 个解决方案

#1


16  

Removing the content-type from the headers resolves this.

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

#2


1  

You will typically want to use both FormParser and MultiPartParser together in order to fully support HTML form data.

您通常希望同时使用FormParser和MultiPartParser,以便完全支持HTML表单数据。

#3


1  

Your error is telling you that the boundary for your multipart/form-data content of your request is invalid - in particular that is is None. This, by design, returns a 400 ("Bad Request") response code. The Error is raised here in the django code.

您的错误告诉您请求的multipart / form-data内容的边界无效 - 特别是None。根据设计,这将返回400(“错误请求”)响应代码。这里在django代码中引发了Error。

To enter that code branch with boundary equal to None means that the boundary option is not specified in the content-type header of your request.

要输入边界等于None的代码分支,意味着未在请求的content-type标头中指定边界选项。

boundary must be specified when using multipart/form-data in content-type as specified in RFC2046 (referred to by RFC2388) - in particular section 5.1.1

在RFC2046(RFC2388引用)中指定的内容类型中使用multipart / form-data时必须指定边界 - 特别是第5.1.1节

The Content-Type field for multipart entities requires one parameter, "boundary".

多部分实体的Content-Type字段需要一个参数“boundary”。

You say it has worked before, so you should check the code that you are using to make the request - something must have changed to mean that the boundary is not specified in the content-type.

你说它之前有用,所以你应该检查你用来发出请求的代码 - 必须改变一些东西,意味着没有在内容类型中指定边界。

N.B. I presume the request is code-generated, as <form method="put"> is invalid HTML and so a request generated by a browser given that HTML would be a GET rather than a PUT.

注:我假设请求是代码生成的,因为

是无效的HTML,因此浏览器生成的请求给出HTML将是GET而不是PUT。

#1


16  

Removing the content-type from the headers resolves this.

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

#2


1  

You will typically want to use both FormParser and MultiPartParser together in order to fully support HTML form data.

您通常希望同时使用FormParser和MultiPartParser,以便完全支持HTML表单数据。

#3


1  

Your error is telling you that the boundary for your multipart/form-data content of your request is invalid - in particular that is is None. This, by design, returns a 400 ("Bad Request") response code. The Error is raised here in the django code.

您的错误告诉您请求的multipart / form-data内容的边界无效 - 特别是None。根据设计,这将返回400(“错误请求”)响应代码。这里在django代码中引发了Error。

To enter that code branch with boundary equal to None means that the boundary option is not specified in the content-type header of your request.

要输入边界等于None的代码分支,意味着未在请求的content-type标头中指定边界选项。

boundary must be specified when using multipart/form-data in content-type as specified in RFC2046 (referred to by RFC2388) - in particular section 5.1.1

在RFC2046(RFC2388引用)中指定的内容类型中使用multipart / form-data时必须指定边界 - 特别是第5.1.1节

The Content-Type field for multipart entities requires one parameter, "boundary".

多部分实体的Content-Type字段需要一个参数“boundary”。

You say it has worked before, so you should check the code that you are using to make the request - something must have changed to mean that the boundary is not specified in the content-type.

你说它之前有用,所以你应该检查你用来发出请求的代码 - 必须改变一些东西,意味着没有在内容类型中指定边界。

N.B. I presume the request is code-generated, as <form method="put"> is invalid HTML and so a request generated by a browser given that HTML would be a GET rather than a PUT.

注:我假设请求是代码生成的,因为

是无效的HTML,因此浏览器生成的请求给出HTML将是GET而不是PUT。