0

Java (tested on JDK11 and JDK17) HttpClient POST request with a "hello" body delivers empty body to the server.

val response = java.net.http.HttpClient.newHttpClient()
    .send(
        java.net.http.HttpRequest.newBuilder()
            .uri(URI.create("http://...."))
            .POST(java.net.http.HttpRequest.BodyPublishers.ofString("hello", StandardCharsets.UTF_8))
            .build(),
        java.net.http.HttpResponse.BodyHandlers.ofString());

No exception throw, response.statusCode() is 202, the content-length header shows 5, but the request body is empty. Tested against multiple server implementations and Postman mock server.

Postman mock server trace

I am executing this code from within a JUnit test.

OpenJDK 64-Bit Server VM Temurin-17.0.5+8 (build 17.0.5+8, mixed mode, sharing)

Update 2023-02-05

Debugging into the JVM, I see two buffers are queued for posting. One contains 9 characters (encoded message start frame?) and the second contains my 5 characters 104, 101, 108, 108, 111 (i.e. hello).
The body publisher then seems to only send the first 9 characters and never my actual 5 character payload. Log follows (please pardon the length):

enter image description here

Full java.net.http log file

2
  • 1
    I'd try setting a Content-Type: text/plain header somehow (but I don't know this particular API). Commented Feb 5, 2023 at 11:15
  • @tevemadar, you got it. I was trying so many different combinations (actually trying to send some binary data with some custom content-type), and removed the content type setting, while creating the minimal repro. HttpRequest::header("Content-Type", "text/plain") Commented Feb 5, 2023 at 13:26

2 Answers 2

2

Per @tevemadar's suggestion, setting a content type (Postman) makes the HttpClient push the data, (or the server to pull the data).

Adding .header("Content-Type", "text/plain") helped made the body appear on the other side. Full code:

    var response =
        java.net.http.HttpClient.newHttpClient()
            .send(
                java.net.http.HttpRequest.newBuilder()
                    .uri(URI.create(uri))
                    .header("Content-Type", "text/plain")
                    .POST(
                        java.net.http.HttpRequest.BodyPublishers.ofString(
                            "hello", StandardCharsets.UTF_8))
                    .build(),
                java.net.http.HttpResponse.BodyHandlers.ofString());

Not setting any content-type on the HttpRequest makes the HttpClient::send method call (or the server-side) ignore the request body (or perceive it as an empty body). There is no error, no warning, nothing in the JDK debug logs.

Setting any (even wrong) content type makes the body come across to the server side.

Sign up to request clarification or add additional context in comments.

Comments

-1

enter image description here

You need to specify HTTP version as HTTP_1_1, the default is HTTP_2

1 Comment

Hi, please don't post code as images. edit your answer and use markdown code blocks (```) instead

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.