Hi there, I'm using ktor with the apache async http client, and I'm sending some POST requests to a server that kept failing because they said the body was empty.
After enabling debug logging, I can see the http client is sending the request in this format:
DEBUG org.apache.http.wire - http-outgoing-0 >> "POST /the/endpoint HTTP/1.1[\r][\n]"
DEBUG org.apache.http.wire - http-outgoing-0 >> "Content-Type: x-www-form-urlencoded[\r][\n]"
DEBUG org.apache.http.wire - http-outgoing-0 >> "Accept: */*[\r][\n]"
DEBUG org.apache.http.wire - http-outgoing-0 >> "Content-Length: 201[\r][\n]"
DEBUG org.apache.http.wire - http-outgoing-0 >> "Host: localhost:8888[\r][\n]"
DEBUG org.apache.http.wire - http-outgoing-0 >> "Connection: Keep-Alive[\r][\n]"
DEBUG org.apache.http.wire - http-outgoing-0 >> "User-Agent: Apache-HttpAsyncClient/4.1.3 (Java/1.8.0_172)[\r][\n]"
DEBUG org.apache.http.wire - http-outgoing-0 >> "[\r][\n]"
DEBUG org.apache.http.wire - http-outgoing-0 >> "url_encoded=form_params&which_are=fine"
Everything seems fine, but for some reason, the client is prepending a line before the form body, with just
\r\n
, which for some reason is tripping the server on the other end.
I don't control the server I'm sending the request to, I don't know if an empty line before the body conforms to the HTTP Spec, but my question is, is there any way to avoid it?
For completeness sake, the way I'm creating the request is as follows:
ApacheHttpClient.request<HttpResponse> {
method = <http://HttpMethod.Post|HttpMethod.Post>
url { takeFrom("<http://some.url/the/endpoint>") }
headers.append("Content-Type", ContentType.Application.FormUrlEncoded.contentSubtype)
this.body = body.formUrlEncode()
}
body is a
List<Pair<String,String>>
and
.formUrlEncode
is the extension method from ktor HttpUrlEncode.kt