Spring File Upload - 'Required request part is not present'

Viewed 59481

I am trying to send POST request to my controller but cannot pass any parameter in any type unless I decide to use JSON. My goal is to pass a String and a file to my controller but I keep getting Required request part 'xxx' is not present error.

@RestController
public class ConfigurationController {
    @PostMapping(value = "/config")
    public ResponseEntity<?> saveEnvironmentConfig(@RequestParam("file") MultipartFile uploadfile){
        return ResponseEntity.ok().body(null);
    }
}

I cannot have file here. Similarly if I try:

@RestController
public class ConfigurationController {
    @PostMapping(value = "/config")
    public ResponseEntity<?> saveEnvironmentConfig(@RequestParam("name") String name){
        return ResponseEntity.ok().body(null);
    }
}

same thing I cannot get name here.

I am sending request via Postman as given in following screenshot:

Postman Request

Postman Request 2

The only header tag is for Authorization. I do not have any Content-Type header, I tried to add multipart/form-data but did not help.

Only way I could pass String parameter is by adding to URL. So following http://localhost:8080/SearchBox/admin/config?name=test works but this is not what I want. I want String and File parameters in Body part.

I also tested via CURL:

curl -X POST -H "Authorization:Bearer myToken" -H "Content-Type:Multipart/form-data" http://localhost:8080/SearchBox/admin/config --data 'pwd=pwd'
curl -X POST -H "Authorization:Bearer myToken"http://localhost:8080/SearchBox/admin/config --data 'pwd=pwd'
curl -H "Authorization:Bearer myToken" -F file=@"/g123.conf" http://localhost:8080/SearchBox/admin/config

Note: I checked similar posts already but did not help This, This, This

12 Answers

I finally solved the issue and sharing my solution in case someone else may face the same problem.

@RestController
@RequestMapping("/")
public class ConfigurationController {

    @Bean
    public MultipartConfigElement multipartConfigElement() {
        return new MultipartConfigElement("");
    }

    @Bean
    public MultipartResolver multipartResolver() {
        org.springframework.web.multipart.commons.CommonsMultipartResolver multipartResolver = new org.springframework.web.multipart.commons.CommonsMultipartResolver();
        multipartResolver.setMaxUploadSize(1000000);
        return multipartResolver;
    }
    @PostMapping(value = "/config", consumes = "multipart/form-data")
    public ResponseEntity<?> saveEnvironmentConfig(@RequestParam("password") String password, @RequestParam("file") MultipartFile submissions)
            throws AdminAuthenticationException, ConfigurationException {
        return ResponseEntity.ok().body(null);
    }
}
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public ResponseEntity<?> upload(@RequestParam(value = "name") String 
name,@RequestParam(value = "file") MultipartFile file){
    // TODO check file is not null and save 
    return new ResponseEntity<>(HttpStatus.valueOf(200));;
}

enter image description here

Add Bean to Config file.

@Bean(name = "multipartResolver")
public CommonsMultipartResolver multipartResolver() {
    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
    multipartResolver.setMaxUploadSize(-1);
    return multipartResolver;

}

If you still getting error, try to remove all headers from postman and drop type of file

In my case the issue was that my csv was not named as per the string set within

@PostMapping("/upload")
    public ResponseEntity<ResponseMessage> uploadFile(@RequestParam("file") MultipartFile file) {

enter image description here

enter image description here

I suspect that main reason is @RequestParam("file") there should be @RequestBody instead.

Consider also the possibility of your request body not reaching your server if it's going through a proxy or any other intermediary that may not support multipart/form-data or even octet-stream content types. Hopefully, this can be solved with extra configuration to work this kind of requests. I can also recommend you to configure a request interceptor so you can log your request before and after your controller, this might help you getting a peek into the request parameters, payload and headers. In my case I realized that the size of my request body was 0, which help me to detect what was causing this error wasn't my spring configuration. Here I leave a useful resource to help you with the logging interceptor which Spring has out of the box. enter link description here

Really not sure what was going on but I removed the code

=======REMOVE BELOW========

@Bean
public MultipartConfigElement multipartConfigElement() {
    return new MultipartConfigElement("");
}

@Bean
public MultipartResolver multipartResolver() {

Added below:

spring.servlet.multipart.max-file-size=-1 
spring.servlet.multipart.max-request-size=-1

And, it starting working magically! Maybe thats how it was with my spring version.

In my case the name attribute of the input type file was not set as "file".

<input type="file" class="file-input" id="fileUpload" name="file">

In my case solution was missing '@EnableWebMvc' annotation on configuration

@EnableWebMvc
@Configuration
internal class BulkOffersUploadConfig {

    @Bean
    fun multipartResolver(): MultipartResolver {
        val multipartResolver = CommonsMultipartResolver()
        multipartResolver.setMaxUploadSize(1000)
        multipartResolver.setMaxUploadSizePerFile(1000)
        return multipartResolver
    }
}

Example of a multipart/form-data JMeter POST:

HTTP Header Manager:
Accept  */*
Content-Type    multipart/form-data; boundary=------------------------jm888
Expect  100-continue
User-Agent  jmeter/3.3
HTTP Request:
uncheck the following:
Redirect Automatically
Follow Redirects
User KeepAlive
Use multipart/form-data for POST
Browser-compatible headers
Select Body Data tab
sample of Body Data:
------------------------jm888
Content-Disposition: form-data; name="microsvc"
Content-Type: application/json

{
    "orgId": 1,
    "ResourceId": "0032ade8-cd59-45b6-8fff-299a63442474",
    "StatusId": "00d8ff46-c688-47cb-85a2-8edfaa1db9a2",
    "TypeId": "a972697e-735e-11e7-8cf7-a6006ad3dba0",
    "Id": "2a1773fe-dbb8-4510-b051-32b2ad823ea1"
}
------------------------jm888
Content-Disposition: form-data; name="payload"; filename="1349.xml"
Content-Type: application/xml

${__FileToString(C:\\jmeterData\\sourceData\\1349.xml,,)}
------------------------jm888--
Related