Spring MVC Framework: MultipartResolver with PUT Method

I am developing spring mvc application with framework 3.2.3.RELEASE

In my application, I handle Multipart using StandardServletMultipartResolver, but with apache commons-fileupload 1.3 everything is the same.

I would like to know why the implementation of the isMultipart method only considers the POST method and not the PUT method. If I want to update the entity and the associated file, I have to do it with POST.

Looking at org.springframework.web.multipart.support.Standard ServletMultipartResolver:

public boolean isMultipart(HttpServletRequest request) { // Same check as in Commons FileUpload... if (!"post".equals(request.getMethod().toLowerCase()) ) { return false; } String contentType = request.getContentType(); return (contentType != null && contentType.toLowerCase().startsWith("multipart/")); } 

and in org.apache.commons.fileupload.servlet.ServletFileU I have:

 public static final boolean isMultipartContent(HttpServletRequest request) { if (!POST_METHOD.equalsIgnoreCase(request.getMethod() )) { return false; } return FileUploadBase.isMultipartContent(new ServletRequestContext(request)); } 

Not vital, really just use the POST method to work PUT. But I want to understand why PUT is not counted!

Thanks for any answer Marco

+6
source share
2 answers

RFC said

http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.6

The URI in the POST request identifies the resource that will process the enclosed object. This resource can be a process of accepting data, a gateway to another protocol, or a separate entity that receives annotations. In contrast, the URI in the PUT request identifies the object enclosed with the request - the user agent knows what the URI is, and the server SHOULD NOT try to apply the request to another resource.

Thus, a PUT request is the only resource.

But multiparts means several resources in one body.

http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html

In the case of messages with several parts, in which one or several different data sets are combined in one body, a β€œmulti-page” Content-Type field should appear in the object header. Then the body must contain one or more "body parts", each of which precedes the encapsulation border, and the last behind the closing border.

Therefore, the semantics of the PUT request do not match multipart data. And the POST is mapped because the requested URI of the POST request is a "private object handler."

+11
source

PUT refers to one resource, for example one file. Thus, by definition, the multi-part form does not correspond to the verb PUT.

Therefore, I assume that these checks for POST could apply to the HTTP specifications: http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html

I think you could argue that PUTting a user object that contains several fields, including one or more files, should be fine, it can still be considered one resource in REST terms, but this is not as it seems to most performers interpret it.

+1
source

Source: https://habr.com/ru/post/959316/


All Articles