Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
davgia opened this issue Apr 8, 2024 · 1 comment

Comments

@davgia
Copy link

davgia commented Apr 8, 2024

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

@davgia
Copy link
Author

davgia commented Apr 10, 2024

UPDATE: the problem seems to be caused by multiple calls of the .add {} function. I have yet to understand why... seems to be related to coroutines

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant