Created
October 21, 2020 23:04
-
-
Save vorburger/d4f11549761c4795d591a00b4148cf89 to your computer and use it in GitHub Desktop.
OkHttp okhttp3.MultipartBody.Part transcribed from Kotlin to Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Turns out I didn't need this after all... ;-) | |
Headers headers = new Headers.Builder().addUnsafeNonAscii("Content-Disposition", buildContentDispositionHeader("file", file.getName())).add("Content-Type", rb.contentType().toString()).build(); | |
return Part.create(headers, rb); | |
// As in okhttp3.MultipartBody.Part.createFormData(String, String, RequestBody), transcribed from Scala (?) | |
private static String buildContentDispositionHeader(String name, String fileName) { | |
StringBuilder sb = new StringBuilder("form-data; name="); | |
appendQuotedString(sb, name); | |
if (fileName != null) { | |
sb.append("; filename="); | |
appendQuotedString(sb, fileName); | |
} | |
return sb.toString(); | |
} | |
// As in private okhttp3.MultipartBody.Part.appendQuotedString(), transcribed from Scala (?) | |
private static void appendQuotedString(StringBuilder sb, String key) { | |
sb.append('"'); | |
for (int i = 0; i < key.length(); i++) { | |
char ch = key.charAt(i); | |
switch (ch) { | |
case '\n': sb.append("%0A"); break; | |
case '\r': sb.append("%0D"); break; | |
case '"': sb.append("%22"); break; | |
default: sb.append(ch); | |
} | |
} | |
sb.append('"'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment