fuel icon indicating copy to clipboard operation
fuel copied to clipboard

Can't pass arabic chracters in inline data part

Open TheEagle0 opened this issue 5 years ago • 5 comments

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.

Screenshot_20190817_180434

TheEagle0 avatar Aug 17 '19 16:08 TheEagle0

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.

SleeplessByte avatar Aug 18 '19 20:08 SleeplessByte

Is there any walkaround ?

muhammadelkady25 avatar Aug 19 '19 15:08 muhammadelkady25

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"))
}

MrFuFuFu avatar Aug 05 '20 22:08 MrFuFuFu

Can you convert the datapart explicitely to utf8 first?

SleeplessByte avatar Aug 06 '20 15:08 SleeplessByte

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();
  }

MrFuFuFu avatar Aug 07 '20 01:08 MrFuFuFu