Tips to Solve ‘MultipartException: Current request is not a multipart request’

Programmers are working on Java language for designing top-notch projects or programs for web applications and websites. It is a language that has recognition all over the world, and that’s the reason programmers are using it, even beginners are also learning it. A Multipart is a container responsible for holding various body parts along with providing a way to set the subparts. It acts as the base class as well. When working on a Java program, you may get ‘MultipartException: Current request is not a multipart request’.

The error makes you worried, but there is no need to get panicked as this is a common exception. And, we are here to provide easy ways to help you. Check out how error occurs

How error occurs

When you work on a project to a make restful controller so that you can upload files, you end up with the exception. Have a look at the program

@RestController
public class MaterialController {

    @RequestMapping(value="/upload", method= RequestMethod.POST)
    public String handleFileUpload(
            @RequestParam("file") MultipartFile file){
        String name = "test11";
        if (!file.isEmpty()) {
            try {
                byte[] bytes = file.getBytes();
                BufferedOutputStream stream =
                        new BufferedOutputStream(new FileOutputStream(new File(name + "-uploaded")));
                stream.write(bytes);
                stream.close();
                return "You successfully uploaded " + name + " into " + name + "-uploaded !";
            } catch (Exception e) {
                return "You failed to upload " + name + " => " + e.getMessage();
            }
        } else {
            return "You failed to upload " + name + " because the file was empty.";
        }
    }
}

After the code, you may have used the postman in order to send a pdf, but in return, you get the error warning.

MultipartException: Current request is not a multipart request

How to Fix ‘MultipartException: Current request is not a multipart request’

The prime reason for the error is using postman for sending a multipart request with no custom Content-Type specified in the header. It makes the header tab blank as a result in the postman. The Postman is used to determine the form-data boundary.

Check out the solution to fix the error warning

Solution 1 – Postman setting

With this, the issue can be resolved. You just need to select the type of file in the body tab of Postman. The ‘Missing start boundary’ is a server error. Check out the solution code

POST /v1/media_photos HTTP/1.1
Host: ****
Authorization: ****
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryp7MA4YWxkTrZu0gW
Cache-Control: no-cache
Postman-Token: 9b823259-ee15-16fe-6aa0-cd094d23286e

----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="photo"; filename="Canon_CanonEOS_4800x3200.jpg"
Content-Type: image/jpeg


----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="metadata"; filename="metadata.json"
Content-Type: application/json


----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="thumbnail"; filename="thumbnail.jpg"
Content-Type: image/jpeg


----WebKitFormBoundaryE19zNvXGzXaLvS5C

CURL command is recommended by Postman. Look below

curl -X POST -H Authorization: *** -H Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryp7MA4YWxkTrZu0gW -H Cache-Control:no-cache -H Postman-Token:298b7786-7f5b-9fce-d290-e51e5d13c915 -F photo= -F metadata= -F thumbnail= http://***/v1/media_photos

The code that is working as CURL code is:

curl --header "Authorization: ***" -F "[email protected]" -F "[email protected]" -F metadata='{"file_size":879394}' http://***/v1/media_photos

Solution 2 – Modify client-side form

If you have a problem requesting to the server that is not a multipart request, then you should modify the client-side form to fix the error warning. Follow the code:

<form action="..." method="post" enctype="multipart/form-data">
  <input type="file" name="file" />
</form>

Solution 3 – Add the Code

You need to add the following code in your application.properties

spring.servlet.multipart.max-file-size=128KB
spring.servlet.multipart.max-request-size=128KB
spring.http.multipart.enabled=false

To add in html form, add the following code

<form method="POST" enctype="multipart/form-data" action="/">

This will resolve the error warning.

Conclusion

Here you are! The tips and solutions are discussed to fix ‘MultipartException: Current request is not a multipart request’. It is completely on you to choose the solution that is suitable for your program.

I hope it helps!

Reference Source: https://dtuto.com/questions/12629/multipartexception-current-request-is-not-a-multipart-request

https://stackoverflow.com/questions/42013087/multipartexception-current-request-is-not-a-multipart-request

Leave a Reply

Your email address will not be published. Required fields are marked *