Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 854
Gokhan Celikkaya Avatar asked Oct 04 '17 10:10

Gokhan Celikkaya


2 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);
    }
}
like image 172
Gokhan Celikkaya Avatar answered Oct 15 '22 01:10

Gokhan Celikkaya


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

like image 38
Francislainy Campos Avatar answered Oct 15 '22 01:10

Francislainy Campos