Issue
I have this code:
@PostMapping("foobar")
public ResponseEntity<SaveLogsResult> foobar(@RequestPart("file") MultipartFile log, @RequestPart("env") MultipartFile json){
return ResponseEntity.ok(fooService.saveFooBar(log, json, UUID.randomUUID().toString()));
}
Two applications send formally correct data to this endpoint, one fails miserably and receives an http status 400.
I set logging.level.org.springframework.web=DEBUG
and can see (among other lines) this:
Required request part 'env' is not present
Resolved [org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'env' is not present]
Completed 400 BAD_REQUEST
To further diagnose this I compared a working (left) and a non-working (right) request. The only different is the mising filename
:
As far as I understand the RFC for Content-Disposition leaving out the filename
is perfectly valid:
Is followed by a string containing the original name of the file transmitted. The filename is always optional and must not be used blindly by the application: path information should be stripped, and conversion to the server file system rules should be done. This parameter provides mostly indicative information. When used in combination with Content-Disposition: attachment, it is used as the default filename for an eventual "Save As" dialog presented to the user.
Is this an error inside Spring ? I use Spring Boot 2.6.2 Unfortunately I can't change the non-working component for a quick test because it is a bought component that doesn't receive bugfixes very often. I think that my problem is different from that described here because the failure only happens in a specific scenario.
Solution
It looks like you have to provide the filename. see this issue
This [The situation in which filename is not present] can lead to inconsistencies, e.g. you would get it with MultipartFile but not with collection or array of MultipartFile, and one can make the opposite argument that it should be rejected.
Why does it matter to have it injected as MultipartFile if it doesn't even have a filename? You could also inject it as InputStream or any other type supported by a registered HttpMessageConverter.
Answered By - hatef alipoor
Answer Checked By - Marilyn (JavaFixing Volunteer)