413 Request Entity Too Large - With Spring Boot and Rest Template
Asked Answered
P

3

6

Using Spring Rest Template to upload a 100 MB file, using a multipart post request.

Client Code:

HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.MULTIPART_FORM_DATA);
    MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();

    map.add("name", chunk.getFilename());
    map.add("filename", chunk.getFilename());
    map.add("flowChunkNumber", chunk.getNumber());
    map.add("flowChunkSize", chunkSize);
    map.add("flowIdentifier", chunk.getIdentifier());
    map.add("flowTotalSize", chunk.getTotalSize());
    map.add("flowCurrentChunkSize", chunk.getSize());
    map.add("file", chunk.getResource() );

    HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<MultiValueMap<String, Object>>(map, headers);


    ResponseEntity<String> response = executeForResponse(
            baseUri().path("/api/v1/uploads/chunks")
                                        .build().toUri(), HttpMethod.POST, entity, String.class);

However the server ( Springs Boot application deployed on Amazon AWS ) returns :

org.springframework.web.client.HttpClientErrorException: 413 Request Entity Too Large
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:614)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:570)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:545)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:466)

Strangely enough I can upload the same file using an angular JS code to the same Spring Boot application.

Though in angular code I use : forceChunkSize : true

Can I do the same in java ?

Parang answered 13/1, 2016 at 9:6 Comment(0)
R
5

Maybe this helps:

SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setBufferRequestBody(false);

RestTemplate template = new RestTemplate(factory);

When sending large amounts of data via POST or PUT, it is recommended to change this property to false

Rhettrhetta answered 13/1, 2016 at 9:18 Comment(2)
NO, This does not help.Parang
Not Worked for me.Mali
B
0

Probably it if not your application fault. I was in the same situation:getting error 413 whathever i configure in my Spring boot application. I tried to reproduce the issue locally and could not do that- locally i did not get the error. And i figured out that actually i got error 413 from Kubernetes ingress controller rather from my app. I found corresponding settings in docs: https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#custom-max-body-size

when I added

nginx.ingress.kubernetes.io/proxy-body-size: "0"

into ingress manifest the error has gone

Baiss answered 20/12, 2023 at 15:13 Comment(0)
G
-2

Add This content your configuration class of application.

@Bean

public MultipartConfigElement multipartConfigElement() {

     MultipartConfigFactory factory = new MultipartConfigFactory();

     factory.setMaxFileSize("200MB");

     factory.setMaxRequestSize("200MB");

     return factory.createMultipartConfig();

}
Grouping answered 26/5, 2018 at 11:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.