question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Have problem when use multipartBody

See original GitHub issue

Hello,

I have a problem when using kohttp to upload an image when using multipartBody I have to upload an image and also needs some data like “name” and “id” like the attached image in Postman. I also need to add “Authorization” in the header. 螢幕快照 2019-07-06 16 21 14

below is my code

val path = "$serverURL"
        val response = httpPost {
            url(path)
            header {
                "content-type" to "multipart/form-data"
                "Authorization" to "Bearer " + myToken
            }
            multipartBody {
                +form("image",image)
                "name" to name
                "id" to "123"

            }
        }.eager()

The image cannot upload and seems like name & id also not send to the server. Can you help me if there is any wrong when I use kohttp to upload the file?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
rybalkinsdcommented, Jul 10, 2019

@rraayy Thanks for posting the comparison with okhttp! Now it’s much easier to detect the difference!

Let me do a comparison:

  1. Request body type: MultipartBody.FORM is provided explicitly in the second example. However, we treat it as multipart/mixed b/c body parts are different.

  2. +form ( ... ) uses null as content type by default. In the second example, MEDIA_TYPE_JPG is provided explicitly.

  3. +form("image", image) will use your exact file.name, in the second example image.jpg is provided explicitly (probably it’s not your case, but want to mention it).

  4. addFormDataPart("name", name) should be provided in a different way:

  5. Order of parts inside the multipart body. It is different in the provided examples. It could lead to a problem on several servers.

As to conclude, I would recommend changing your request to the following:

   val response = httpPost {
            url(MYPATH)
            header {
                "Authorization" to "Bearer " + MYTOKEN
            }

            multipartBody(MULTIPART_FORM_TYPE) {
                +FormDataPart("image", "image.jpg", RequestBody.create(MEDIA_TYPE_JPG, image))
                +FormDataPart("name", null, RequestBody.create(null, name))
            }
    }
1reaction
rybalkinsdcommented, Jul 11, 2019

Closing this issue. Enhancements connected with UX feedback will be implemented in #126

Read more comments on GitHub >

github_iconTop Results From Across the Web

Retrofit @body with @multipart having Issue - Stack Overflow
Everything works great. Problem arises while adding @Part MultipartBody.Part file . It throws Expected BEGIN_OBJECT but was STRING at line 1 column 1...
Read more >
Content-Disposition - HTTP - MDN Web Docs
The Content-Type defining the boundary of the multipart body. The FormData interface used to manipulate form data for use in the XMLHttpRequest ...
Read more >
Display progress of multipart request with Retrofit and RxJava
To use the solution you have to create ProgressResponseBody class which extends ResponseBody and gets progress listener in the constructor.
Read more >
Retrofit 2 — How to Upload a Dynamic Amount of Files to Server
Upload a Dynamic Amount of Files. The solution for this problem is to pass a List or Array of MultipartBody.Part objects. Retrofit and...
Read more >
JAX-RS Multiparts - Apache CXF
You don't have to use Multipart annotations. Uploading files with Client API. At the moment the only way to upload a file is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found