forked from loafle/openapi-generator-original
Jersey2/3 - Probe content type for multipart upload parts (#14965)
* Probe content type for multipart form uploads since many servers require each part to correctly identify its type. * Update samples * Add explanatory comment * Update samples with comment
This commit is contained in:
@@ -807,7 +807,17 @@ public class ApiClient extends JavaTimeFormatter {
|
||||
File file = (File) param.getValue();
|
||||
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey())
|
||||
.fileName(file.getName()).size(file.length()).build();
|
||||
multiPart.bodyPart(new FormDataBodyPart(contentDisp, file, MediaType.APPLICATION_OCTET_STREAM_TYPE));
|
||||
|
||||
// Attempt to probe the content type for the file so that the form part is more correctly
|
||||
// and precisely identified, but fall back to application/octet-stream if that fails.
|
||||
MediaType type;
|
||||
try {
|
||||
type = MediaType.valueOf(Files.probeContentType(file.toPath()));
|
||||
} catch (IOException | IllegalArgumentException e) {
|
||||
type = MediaType.APPLICATION_OCTET_STREAM_TYPE;
|
||||
}
|
||||
|
||||
multiPart.bodyPart(new FormDataBodyPart(contentDisp, file, type));
|
||||
} else {
|
||||
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey()).build();
|
||||
multiPart.bodyPart(new FormDataBodyPart(contentDisp, parameterToString(param.getValue())));
|
||||
|
||||
Reference in New Issue
Block a user