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.

Apache HTTP Client does not send Content-Length header if body is empty content

See original GitHub issue

Ktor 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:

  1. 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/")
  1. Run on jvm
  2. 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:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
christophpicklcommented, Feb 13, 2020

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/1644

any feedback on this would be highly appreciated.

1reaction
SIY1121commented, Sep 11, 2019

No. It’s a client issue, not a server issue. I tried to send a empty put request to a third-party api server.

val apacheClient = HttpClient(Apache)
apacheClient.put<HttpResponseData>("http://example.com/") 

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

For example, a Content-Length header field is normally sent in a POST request even when the value is 0 (indicating an empty payload body).

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Apache HttpClient is not showing Content-Length and ...
OK, yes it works only if I sent the Accept-Encoding: identity header, otherwise HttpClient will not show the Content-Length header :( . ·...
Read more >
Known Problems in Clients - Apache HTTP Server
POST without Content-Length. In certain situations Navigator 3.01 through 3.03 appear to incorrectly issue a POST without the request body. There is no...
Read more >
HTTP/1.1: Status Code Definitions
If a Content-Length header field is present in the response, its value MUST match the actual number of OCTETs transmitted in the message-body....
Read more >
RFC 2616: Hypertext Transfer Protocol -- HTTP/1.1
The Content-Length header field MUST NOT be sent if these two lengths are different (i.e., if a Transfer-Encoding Fielding, et al. Standards Track...
Read more >
HTTP Protocol Compliance - Apache HTTP Server Version 2.5
However, when HTTP/1.0 requests are processed, and no Content-Length is specified, the only mechanism available to the server to indicate the end of...
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