Apache HTTP Client does not send Content-Length header if body is empty content
See original GitHub issueKtor Version and Engine Used (client or server and name) io.ktor:ktor-client-core:1.2.4 io.ktor:ktor-client-core-jvm:1.2.4 io.ktor:ktor-client-apache:1.2.4
Describe the bug Apache HTTP Client does not send Content-Length header if body is empty content. CIO send the header correctly.
To Reproduce Steps to reproduce the behavior:
- Write the following
val apacheClient = HttpClient(Apache)
apacheClient.put<HttpResponseData>("http://example.com/")
apacheClient.post<HttpResponseData>("http://example.com/")
apacheClient.patch<HttpResponseData>("http://example.com/")
- Run on jvm
- The sent header does not contain Content-Length
{
"accept-charset": "UTF-8",
"accept": "*/*",
"user-agent": "Ktor client",
"host": "example.com",
"connection": "Keep-Alive"
}
Some endpoints that do not require body return 411 Length Required.
Expected behavior
Content-Length: 0
Should be sent.
Apache seems not to send Content-Length
if entity is null.
Following code sent the header correctly.
apacheClient.put<HttpResponseData>("http://example.com/"){
body = ""
}
The relevant part of the code in ktor: https://github.com/ktorio/ktor/blob/f3858b2bd95dcb35aa7755451403e7cab1dd678c/ktor-client/ktor-client-apache/jvm/src/io/ktor/client/engine/apache/ApacheRequestProducer.kt#L122-L155
and org.apache.http.client.methods.RequestBuilder 's build() method.
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (2 by maintainers)
Top GitHub Comments
hi guys, i’ve encountered this bug in one of our production systems. we temporarily added the solution (hack?) of simply setting the body to
""
. in order to fix it properly i’ve created a PR which should do the job: https://github.com/ktorio/ktor/pull/1644any feedback on this would be highly appreciated.
No. It’s a client issue, not a server issue. I tried to send a empty put request to a third-party api server.
However, the server returns 411 Length Required. The cause is Ktor client with Apache engine didn’t send content-length header.
content-length or transfer-encoding headers should be present.
RFC 7230, section 3.3.2: Content-Length