Stefan Oltmann
02/26/2024, 8:26 AMUnexpected char 0xfc at 11 in Dropbox-API-Arg value: {"path":"/süße vögelchen/photo_9.jpg"}
).
In the forums they say this:
For these calls with the parameters in the header, you need to escape these characters. That is, when you use the “Dropbox-API-Arg” header, you need to make it “HTTP header safe”. This means using JSON-style “\uXXXX” escape codes for the character 0x7F and all non-ASCII characters.Is there anything already build-in to achieve that? Maybe in Ktor or kotlinx-serialization? How would KMP code look like to do this?
Stefan Oltmann
02/26/2024, 8:27 AMfun escapeString(input: String): String {
val sb = StringBuilder()
for (char in input) {
when {
char == 0x7F.toChar() || char.toInt() > 127 -> {
// Escape non-ASCII characters using JSON-style "\uXXXX" escape codes
sb.append("\\u").append(String.format("%04x", char.toInt()))
}
else -> sb.append(char)
}
}
return sb.toString()
}
Sam
02/26/2024, 8:46 AMSam
02/26/2024, 8:47 AMSam
02/26/2024, 8:47 AMStefan Oltmann
02/26/2024, 8:50 AMso running it through a JSON encoder wouldn't cover all the characters you want to encode.Yes, it already runs through kotlinx-serialization, but that doesn't change umlauts. And there seems to be no option to do so.
Sam
02/26/2024, 9:00 AMStefan Oltmann
02/26/2024, 9:01 AMSam
02/26/2024, 9:02 AMSam
02/26/2024, 9:02 AMSam
02/26/2024, 9:03 AMSam
02/26/2024, 9:05 AMStefan Oltmann
02/26/2024, 9:05 AMStefan Oltmann
02/26/2024, 9:06 AMSam
02/26/2024, 9:07 AMfun escapeString(input: String): String {
val sb = StringBuilder()
for (char in input) {
if (char.code in 32..127) {
sb.append(char)
} else {
sb.append("\\u" + char.code.toHexString().takeLast(4))
}
}
return sb.toString()
}
Sam
02/26/2024, 9:08 AMStefan Oltmann
02/26/2024, 9:09 AMStefan Oltmann
02/26/2024, 9:12 AMStefan Oltmann
02/26/2024, 9:13 AMStefan Oltmann
02/26/2024, 9:21 AMSam
02/26/2024, 9:21 AMRuckus
02/26/2024, 2:42 PMbuildString
function:
fun escapeString(input: String) = buildString {
for (char in input) {
if (char.code in 32..<127) {
append(char)
} else {
append("\\u" + char.code.toHexString().takeLast(4))
}
}
}
Or, if you're feeling really spicy:
fun String.escape() = buildString {
for (char in this@escape) {
if (char.code in 32..<127) {
append(char)
} else {
append("\\u" + char.code.toHexString().takeLast(4))
}
}
}
(also note I changed it to 32..<127
to match the error, which says 0x7f
needs to be converted as well)