fuel
fuel copied to clipboard
Can't pass arabic chracters in inline data part
I am calling an API with fuel like this
suspend fun uploadMedia(
dir: File,
fileName: String,
title: String,
parentId: String
): UploadMediaResponse {
return Fuel.post(
Uri.parse(BASE_URL)
.buildUpon()
.appendPath(“upload”).appendPath(“media”)
.toString()
).upload()
.add(FileDataPart.from(dir, fileName))
.add(InlineDataPart(parentId, “parent_id”))
.add(InlineDataPart(title, “title”))
.awaitResponse(UploadMediaResponse.Deserializer(), cc).third
}
@Test
fun uploadMedia() = runBlocking(Dispatchers.IO) {
val fileName = “fatihah.mp3”
val inputRawStream: InputStream = app.resources.openRawResource(R.raw.fatihah)
File(“${app.dataDir.absolutePath}/$fileName”).outputStream()
.use { output: FileOutputStream -> inputRawStream.use { input -> input.copyTo(output) } }
val res = api.uploadMedia(app.dataDir, fileName, “مقطع تجريبي“, “main-media”)
Logger.logI(TAG, “$res”)
Unit
}
When passing title to API in Arabic it gives me an error : java.net.ProtocolException: expected * bytes but received * but when title is in English it works just fine.
Interesting! Somewhere, we're probably not correctly converting the bytes (to utf8) BEFORE counting them, so the before count doesn't match the after count.
Is there any walkaround ?
Having the same issue when dataPart
contains emojis. Any solutions? (dataPart
is a JSON string in this case)
if (request is UploadRequest && !dataPart.isNullOrEmpty()) {
request.add(InlineDataPart(dataPart, "custom_questionnaire"))
}
Can you convert the datapart explicitely to utf8 first?
Hi @SleeplessByte thanks for asking, Yeah. I also tried that. but no luck, still have the issue.
request.add(InlineDataPart(dataPart, "custom_questionnaire", contentType = "multipart/form-data; charset=utf-8"))
dataPart = Moshi.Builder().build().adapter(CustomQuestionnaireAnswerListModel::class.java).toJson(customQuestionnaireAnswer)
About the toJson method from Moshi: it uses utf8 as the default and I didn't change that anywhere in my code.
@CheckReturnValue public final String toJson(@Nullable T value) {
Buffer buffer = new Buffer();
try {
toJson(buffer, value);
} catch (IOException e) {
throw new AssertionError(e); // No I/O writing to a Buffer.
}
return buffer.readUtf8();
}