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:
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
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);
}
}
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) {
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With