users@jersey.java.net

[Jersey] Re: POST without body

From: Owen Jacobson <owen.jacobson_at_grimoire.ca>
Date: Wed, 22 Jan 2014 14:57:42 -0500

On Jan 22, 2014, at 13:59, Erik Hennum <efhennum_at_gmail.com> wrote:

> Hi, Jersey Folk:
>
> We're trying to upgrade from 1.x to 2.x
>
> On 1.x, we make a few POST requests that provides no body or Content-Type
> header but accept a response.

Does your client provide a Content-Length header, or send a FIN immediately after the header-terminating second newline? Can you show us the bare HTTP requests?

> I note that the following workaround was suggested when this question
> came up recently:
>
> Client.newClient().target(...).request().post(Entity.entity(null, "foo/bar"));
> http://markmail.org/message/4bmg5slbrfzgtusu
>
> However, this workaround fails in our environment. It appears that, because
> of the Content-Type header, the server waits for the client to send the body
> until the request times out.

Some testing with the example at the end of this post suggests that Jersey’s client does the wrong thing in this case, and neither sends Content-Length nor a terminating FIN after the (empty) request body. That’s arguably a bug; the server implementation sends back a No Content response in this case.

The following code demonstrates the issue under 2.5:

package com.example.jaxrs.client;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;

public class NoEntityPost {
        public static void main(String[] args) {
                Client c = ClientBuilder.newClient();
                Entity<Object> entity = Entity.entity(null, "application/x-ample");
                c.target("http://localhost:8990/").request().post(entity);
        }
}

A tiny Python interactive “server” shows the client failing to end the request:

>>> client.recv(1024)
'POST / HTTP/1.1\r\nContent-Type: application/x-ample\r\nUser-Agent: Jersey/2.5 (HttpUrlConnection 1.7.0_51)\r\nHost: localhost:8990\r\nAccept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\r\nConnection: keep-alive\r\n\r\n'
>>> client.recv(1024)
(waits indefinitely)