简体   繁体   中英

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: 在此处输入图片说明

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.

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

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM