Have problem when use multipartBody
See original GitHub issueHello,
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.
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:
- Created 4 years ago
- Reactions:1
- Comments:12 (8 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@rraayy Thanks for posting the comparison with okhttp! Now it’s much easier to detect the difference!
Let me do a comparison:
Request body type:
MultipartBody.FORM
is provided explicitly in the second example. However, we treat it asmultipart/mixed
b/c body parts are different.+form ( ... )
usesnull
as content type by default. In the second example,MEDIA_TYPE_JPG
is provided explicitly.+form("image", image)
will use your exactfile.name
, in the second exampleimage.jpg
is provided explicitly (probably it’s not your case, but want to mention it).addFormDataPart("name", name)
should be provided in a different way: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:
Closing this issue. Enhancements connected with UX feedback will be implemented in #126