17

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

Gokhan Celikkaya
  • 716
  • 1
  • 7
  • 17
  • For others with a similar problem, this may be the solution you're looking for: https://stackoverflow.com/questions/40488585/postman-required-request-part-file-is-not-present – drew Apr 03 '18 at 21:03
  • You have to add bean multipartResolver in your addConfig in case you using spring mvc. Like this http://www.baeldung.com/spring-file-upload – Roman Klimov Apr 22 '18 at 21:45

12 Answers12

25

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);
    }
}
Gokhan Celikkaya
  • 716
  • 1
  • 7
  • 17
  • 3
    This does not work for me in Spring 2.0.0. In fact, adding those beans creates exactly that error. Adding none won't cause an error. – Impulse The Fox Mar 21 '18 at 16:14
  • @Gokhan why do you use multipartResolver() and multipartConfigElement() method. – Priyantha Feb 12 '19 at 10:54
  • adding @Bean public MultipartConfigElement multipartConfigElement() { return new MultipartConfigElement(""); } and works in Spring Boot 2.1.6 thanks! – Marco López Jul 27 '19 at 04:01
  • please can you share the Junit test case for this controller. I am finding difficulty in sending file as post request – kiranNswamy Mar 03 '20 at 12:39
4

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

Francislainy Campos
  • 3,462
  • 4
  • 33
  • 81
2
@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

baba
  • 265
  • 3
  • 9
  • 1
    Same :( {"message":"Required String parameter 'title' is not present","status":400,"errors":["Required String parameter 'title' is not present"]} – Gokhan Celikkaya Oct 04 '17 at 11:21
  • change the parameter format for "file" from Text to File from drop down menu and select the file you wanted to upload – baba Oct 04 '17 at 11:58
  • update the first parameter to this @RequestParam(value = "name") String name – baba Oct 04 '17 at 12:28
  • I already changed the name. It seems I required Bean definition. Please see my answer. It solved my problem. Thank you for helping – Gokhan Celikkaya Oct 04 '17 at 12:33
  • or you can add spring.http.multipart.max-file-size=100Mb in applications.properties configuration file – baba Oct 04 '17 at 13:27
  • I dont know why, but interchanging the params, i.e. putting multipart file first and string next solved the issue for me – tarunkt Jan 03 '18 at 14:27
2

Add Bean to Config file.

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

}
1

While this was not the original poster's problem, if you found this question and none of the above solutions worked for you, check your Postman headers; if "Content-Type" header is set, this may cause the error above.

Remove the "Content-Type" header and it may resolve the issue above.

See this question for more information:

Postman : Required request part 'file' is not present

drew
  • 2,949
  • 3
  • 25
  • 27
1

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

  • Thanks, solved my problem of testing with webtestclient. I removed header of Content-Type. I think webtestclient will add it – WesternGun Sep 01 '20 at 13:05
1

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.

10101010
  • 607
  • 8
  • 24
0

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

  • 1
    `@RequestBody @RequestParam("name") String name` did not work. When I try `@RequestBody String name` name variable get following value: `------WebKitFormBoundary6WJHhVqqZrh8qeAX Content-Disposition: form-data; name="name" test ------WebKitFormBoundary6WJHhVqqZrh8qeAX-- ` – Gokhan Celikkaya Oct 04 '17 at 11:13
  • Upvoting this answer as through it realised the name I was expecting for my file ("file") was not matching what I was setting on Postman. Thank you. – Francislainy Campos Sep 25 '20 at 01:44
0

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

darkconeja
  • 348
  • 3
  • 7
0

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">
Suvo
  • 31
  • 1
  • 2
  • 4
-1

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
    }
}
Aik
  • 1
-2

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--
Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
teenaanie
  • 7
  • 2