Skip to content

Error: The inputs have already been written to an output stream and can not be consumed again. #884

@davgia

Description

@davgia

Hi, I have an Android application that needs to upload a local temp file to a server. At runtime I receive the following error:

The inputs have already been written to an output stream and can not be consumed again.
	com.github.kittinunf.fuel.core.FuelError$Companion.wrap(FuelError.kt:86)
	com.github.kittinunf.fuel.core.FuelError$Companion.wrap$default(FuelError.kt:83)
Caused by: java.lang.IllegalStateException: The inputs have already been written to an output stream and can not be consumed again.
	com.github.kittinunf.fuel.core.requests.UploadBody.writeTo(UploadBody.kt:100)
	com.github.kittinunf.fuel.toolbox.HttpClient.setBodyIfDoOutput(HttpClient.kt:273)
	com.github.kittinunf.fuel.toolbox.HttpClient.sendRequest(HttpClient.kt:140)
	com.github.kittinunf.fuel.toolbox.HttpClient.doRequest(HttpClient.kt:79)
	com.github.kittinunf.fuel.toolbox.HttpClient.executeRequest(HttpClient.kt:37)
	com.github.kittinunf.fuel.core.requests.RequestTask.executeRequest(RequestTask.kt:23)
	com.github.kittinunf.fuel.core.requests.RequestTask.call(RequestTask.kt:44)
	com.github.kittinunf.fuel.core.requests.RequestTask.call(RequestTask.kt:14)
	com.github.kittinunf.fuel.core.requests.RequestTaskCallbacks.call(RequestTaskCallbacks.kt:28)
	com.github.kittinunf.fuel.core.requests.RequestTaskCallbacks.call(RequestTaskCallbacks.kt:20)
	java.util.concurrent.FutureTask.run(FutureTask.java:266)
	java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
	java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
	java.lang.Thread.run(Thread.java:764)

The android version is the following:

android {
    defaultConfig {
        compileSdk 34
        minSdkVersion 21
        targetSdkVersion 34
    }
}

Those are my dependencies:

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-reflect:1.9.21"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.8.0"

    implementation 'com.github.kittinunf.fuel:fuel:2.3.1'
    implementation 'com.github.kittinunf.fuel:fuel-android:2.3.1'
    implementation 'com.github.kittinunf.fuel:fuel-gson:2.3.1'
    implementation 'com.github.kittinunf.fuel:fuel-coroutines:2.3.1'
}

And this is the code I'm using to upload:

private suspend fun uploadFile(localPath: String): String {
    val bearerToken = authenticate() //retrieves a jwt bearer token
    val url = "***REDACTED***" //simple upload endpoint that accepts multipart/form-data file and saves it elsewhere

    withContext(Dispatchers.IO) {
        Fuel.upload(url, Method.POST)
            .authentication()
            .bearer(bearerToken)
            .upload()
            .add { FileDataPart(File(localPath), name = "file", filename = "input.pdf", contentType = PDF_MIME_TYPE) }
            .progress { readBytes, totalBytes ->
                if (readBytes > 0 && totalBytes > 0) {
                    val progress: Int = (readBytes.toFloat() / totalBytes.toFloat() * 100).toInt()
                    Log.d(TAG, "Upload progress: $progress%")
                }
            }
            .responseString { _, _, result ->
                result.fold({
                    Log.d(TAG, "Upload completed")
                }, { error ->
                    Log.e(TAG, "Unable to upload the file", error)
                })
            }
            .awaitUnit()
    }
}

The file localPath should not be locked by anyone else. I've even make a copy of it and pass the copy to Fuel to double check but it returns the same error. Could it be a bug?

Thanks in advance

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions