Boundary info in Content-Type multipart request header not parsed correctly because of charset [SPR-17030]
See original GitHub issuebissorc opened SPR-17030 and commented
I’m using Spring Rest Template to upload a file. Code looks as follows:
MultipartBodyBuilder builder = new MultipartBodyBuilder();
builder.part("file", "2;3;4".getBytes());
MultiValueMap<String, HttpEntity<?>> body = builder.build();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Collections.singletonList(MediaType.ALL));
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity(body, headers);
RestTemplate restTemplate = new RestTemplate();
restTemplate.postForLocation("***", requestEntity, String.class);
This leads to the following raw request:
POST *** HTTP/1.1
Accept: */*
Content-Type: multipart/form-data;boundary=059h2BBM-KlM_XP2rY8W1X3_jnzFLcYY;charset=UTF-8
User-Agent: Java/1.8.0_121
Host: ***:***
Connection: keep-alive
Content-Length: 187
--059h2BBM-KlM_XP2rY8W1X3_jnzFLcYY
Content-Disposition: form-data; name="file"
Content-Type: text/plain;charset=UTF-8
Content-Length: 5
2;3;4
--059h2BBM-KlM_XP2rY8W1X3_jnzFLcYY—
The problem is the following line:
Content-Type: multipart/form-data;boundary=059h2BBM-KlM_XP2rY8W1X3_jnzFLcYY;charset=UTF-8
Spring Rest Template adds ;charset=UTF-8
to the content type and this leads to problems when. I found no possibility to remove the charset. From my point of view this is a bug since the charset should not be part of the content type?!
Affects: 5.0.7
Issue Links:
- #21599 Multipart: Invalid boundary with RestTemplate (“is duplicated by”)
Referenced from: commits https://github.com/spring-projects/spring-framework/commit/f89511e7fe9c32ad28de2c00a6197ca9ad6ce937, https://github.com/spring-projects/spring-framework/commit/390bb871d85f56aaeed98aeb45d688fcf35af8e7
Issue Analytics
- State:
- Created 5 years ago
- Comments:13
Top Results From Across the Web
Content type 'multipart/form-data;boundary=----...;charset=UTF ...
In case of MultipartFile you can't use JSON data so you can't use @RequestBody. Try with @ModelAttribute annotation. Working sample :
Read more >Charset Content-Type Option not respected when using ...
When invoking Oracle Service Bus (OSB) using a multipart request where the charset is only specified in the part headers, OSB does not...
Read more >multipart/form-data text/plain resttemplate - You.com
How to send Multipart form data with restTemplate Spring-mvc ... multipart request header not parsed correctly because of charset [SPR-17030]#21568.
Read more >Description of multipart/mixed Internet message format
A multipart/mixed MIME message is composed of a mix of different data types. Each body part is delineated by a boundary. The boundary...
Read more >Troubleshooting issues with adding attachments to rest ...
The Content-Type header should be set to multipart/form-data or multipart/mixed. ... does not recognize the boundary with quotes in multipart/form-data.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
bissorc commented
Brian Clozel: for the H2O server this will work:
Joe Amoros commented
Brian Clozel - Hmm bummer. Just gave it a try. I see the change, but still getting the error. Here’s the logging and below, the error. I removed the body from the logging to save space. I’ll keep digging. Thanks again!
From Apache/ModSecurity
A little extra info, this is the config that is failing in modsecurity
One last bit, if I add a whitelist in the modsecurity config like so:
The file uploads, but we still get a warning:
Cheers!