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.

Content-Length is not set when using MultiPartFormDataContent

See original GitHub issue

Ktor Version - 1.1.1 Ktor Engine Used(client or server and name) - Client - okhttp JVM Version - 1.8.0 Operating System - Android Request: PUT content-type:  Multipart/form-data Ktor Client Engine: Okhttp & Android

As with the title, the Content-Length is not set when using MultiPartFormDataContent. We get transfer-encoding:  chunked instead. It is important to upload files to Django/WSGI powered backends as it is rather difficult about handling file uploads without Content-Length and other http clients such as Retrofit doesn’t have this problem because it added the content-length in  multipart/form-data uploads

as
 GlobalScope.launch(Dispatchers.Main) {
            try {
                client.submitForm {
                    url("http://192.168.1.47:8000/api/images/")
                    method = HttpMethod.Put
                    header(HttpHeaders.Authorization, "JWT $token")

                    body = MultiPartFormDataContent(
                        formData {
                            append(
                                "image",
                                data,
                                Headers.build {
                                    append(HttpHeaders.ContentType, "image/jpg")
                                    append(HttpHeaders.ContentDisposition, " filename=hello.jpg")
                                }
                            )
                        }
                    )
                }
            } catch (e: Exception){
                Log.d(this@MainActivity::class.java.simpleName, e.message)
            }
        }

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:11
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
kmatiascommented, Jan 9, 2019

I am encountering this issue as well. The header of the multipart form request has transfer-encoding: chunked instead of Content-Length which the server requires.

Has anyone looked into this issue yet?

0reactions
charleston10commented, Jan 13, 2021

I’ve this same problem in version 1.4.2, using android ktor in KMM How can I solve?

 override suspend fun postImage(filePath: String) {
        val inputFile = InputFile().getFile(filePath)
        val file = InputProvider { inputFile }

        httpClient
            .post<HttpResponse>(UrlProvider.posts) {
                headers { append("Accept", FORM_ACCEPT_MULTIPART) }
                body = MultiPartFormDataContent(formData {
                    append(
                        "file",
                        InputProvider(file.size) { buildPacket { writeFully(inputFile.readBytes()) } },
                        Headers.build {
                            append(HttpHeaders.ContentType, ContentType.Image.PNG.toString())
                            append(
                                HttpHeaders.ContentDisposition,
                                ContentDisposition.File.withParameter(
                                    ContentDisposition.Parameters.FileName,
                                    "test"
                                )
                            )
                        })
                })
            }
    }

Log

:method: POST
:path: /posts
:authority: 192.168.0.1:8080
:scheme: https
accept: multipart/form-data
accept-charset: UTF-8
content-type: multipart/form-data; boundary=-4944c64b-2fa78d1a-148ded0d-fbd9d7d669a8d-1709412d-2d6dc51e-185d27aa13
cache-control: public, max-age=5
accept-encoding: gzip

---4944c64b-2fa78d1a-148ded0d-fbd9d7d669a8d-1709412d-2d6dc51e-185d27aa13
Content-Disposition: form-data; name=file; file; filename=test
Content-Type: image/png


---4944c64b-2fa78d1a-148ded0d-fbd9d7d669a8d-1709412d-2d6dc51e-185d27aa13--
Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - MultipartFormDataContent not adding Content-Type in ...
I have a simple HttpClient that does a multipart/form-data post to an endpoint. using ...
Read more >
Multipart Form Post in C# - Brian Grinstead
I first tried using the WebClient UploadFile method, but it didn't fit my needs because I wanted to upload form values (id, filename,...
Read more >
How do I send a POST request with Content-Length Header?
The data length must be specified in bytes. In this POST request with a Content-Length Header Example, we make a POST request to...
Read more >
C# - How to send a file with HttpClient - MAKOLYTE
Shows how to send a file in a multipart/form-data request using HttpClient by loading it into a MultipartFormDataContent object.
Read more >
[C#] Upload pictures with HttpClient - data not sending correctly
I'm trying to upload a JPG picture on my server (typical url : http://www.domain.com/upload/{idUser}) and to do that I have been using ......
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