Spring File Upload - 'Required request part is not present'
Asked Answered
T

12

18

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

Tequilater answered 4/10, 2017 at 10:59 Comment(2)
For others with a similar problem, this may be the solution you're looking for: #40489085Sharpeared
You have to add bean multipartResolver in your addConfig in case you using spring mvc. Like this baeldung.com/spring-file-uploadRafaelle
T
27

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);
    }
}
Tequilater answered 4/10, 2017 at 12:25 Comment(4)
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.Pernambuco
@Gokhan why do you use multipartResolver() and multipartConfigElement() method.Gilberte
adding @Bean public MultipartConfigElement multipartConfigElement() { return new MultipartConfigElement(""); } and works in Spring Boot 2.1.6 thanks!Lukey
please can you share the Junit test case for this controller. I am finding difficulty in sending file as post requestNickinickie
E
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

Edme answered 25/9, 2020 at 1:51 Comment(0)
V
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

Volturno answered 4/10, 2017 at 11:15 Comment(6)
Same :( {"message":"Required String parameter 'title' is not present","status":400,"errors":["Required String parameter 'title' is not present"]}Tequilater
change the parameter format for "file" from Text to File from drop down menu and select the file you wanted to uploadVolturno
update the first parameter to this @RequestParam(value = "name") String nameVolturno
I already changed the name. It seems I required Bean definition. Please see my answer. It solved my problem. Thank you for helpingTequilater
or you can add spring.http.multipart.max-file-size=100Mb in applications.properties configuration fileVolturno
I dont know why, but interchanging the params, i.e. putting multipart file first and string next solved the issue for meGleich
S
2

Add Bean to Config file.

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

}
Staminody answered 31/10, 2018 at 7:39 Comment(0)
B
2

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.

Bakst answered 21/5, 2022 at 3:59 Comment(0)
S
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

Sharpeared answered 3/4, 2018 at 21:1 Comment(0)
H
1

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

Hyacinth answered 18/8, 2020 at 10:51 Comment(1)
Thanks, solved my problem of testing with webtestclient. I removed header of Content-Type. I think webtestclient will add itBelleslettres
C
0

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

Crankcase answered 4/10, 2017 at 11:4 Comment(2)
@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-- Tequilater
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.Edme
C
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

Chrestomathy answered 17/1, 2019 at 2:35 Comment(0)
S
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">
Spellbinder answered 1/7, 2022 at 12:25 Comment(0)
J
-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
    }
}
Jerilynjeritah answered 30/10, 2019 at 8:49 Comment(0)
S
-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--
Salmagundi answered 2/5, 2022 at 12:55 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.