Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1.2K
Views
How to POST multipart/form-data using Fuel for Kotlin?

I need to send a POST request to a server. I'm supposed to pass some parameters and an image. I am able to do this from Postman, but I can't do this on my Android app (latest SDK) using Fuel.

This is the code I'm using:

val formData = listOf("name" to "name")
val (_, _, result) = Fuel.upload("http://10.0.2.2:3000/test", parameters = formData)
                         .source { request, url -> imageFile } // type is File
                         .responseObject<CustomResponse>()

This is the postman screenshot: enter image description here

I don't have access to the backend code, just some logs. It seems the request body is empty and the file also doesn't get uploaded. How can I do this? I'm at a loss.

I also tried passing the parameters as a jsonBody, this does submit the body, but the content type is not multipart/form-data and the image is still missing.

This JS code works:

let formData = new FormData();
formData.append('name', 'name');
formData.append('image', this.file);
axios.post(`${API_URL}/test`, formData, {
    headers: {
        'Content-Type': 'multipart/form-data'
    }
}).then(console.log).catch(console.log)

Edit: I also tried passing the file as a DataPart, still nothing.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

After some struggle I found out what would work:

val file = FileDataPart.from("path_to_your_file", name = "image")
val (_, _, result) = Fuel.upload("http://10.0.2.2:3000/test")
                 .add(file)
                 .responseObject<CustomResponse>()

I didn't need name-name part in my case but I would try to add InlineDataPart

over 4 years ago · Santiago Trujillo Report

0

After some try finally I found the solution. Try this I think it will help.

    val params = listOf("email" to "test@email.com", "pass" to "123456")
    Fuel.upload("http://your-api-url.com/login", Method.POST, params)
        .responseString { _, _, result ->               
            when (result) {
                is Result.Failure -> {
                    print(result.getException().toString())
                }
                is Result.Success -> {
                    val data = result.get()
                    print(data)
                }
            }
        }
over 4 years ago · Santiago Trujillo Report

0

I managed to send a POST request to a server. I passed a parameter and an image.

        //Prepare POST body
        val postBody = listOf("name" to "name")

        //Call the API.
        val (_, _, result) = Fuel.upload("http://10.0.2.2:3000/test", Method.POST , postBody)
            .add(BlobDataPart(myInputStream, name = "image", filename = "default.jpg", contentType = "image/jpeg"))
            .responseString()

        //If failed, then print exception. If successful, then print result.
        when (result) {
            is Result.Failure -> {
                println(result.getException())
            }
            is Result.Success -> {
                println(result.get())
            }
        }

You can read the related documentation here.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!