Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

1.2K
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda