Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jersey http client custom request method

With the following code, using jersey:

    <groupId>com.sun.jersey.contribs</groupId>
    <artifactId>jersey-apache-client4</artifactId>
    <version>1.13-b01</version>

I have issues using custom request methods, like FOOBAR, PATCH, SEARCH, etc. Those which do not exist in httpUrlConnection.

 DefaultClientConfig config = new DefaultClientConfig();
 config.getProperties().put(URLConnectionClientHandler.PROPERTY_HTTP_URL_CONNECTION_SET_METHOD_WORKAROUND, true);

 Client c = Client.create(config);
 Form f = new Form();
 f.add("id", "foobar");

 WebResource r = c.resource("http://127.0.0.1/foo");
 String methodName = "foobar";
 String response = r.type(MediaType.APPLICATION_FORM_URLENCODED_TYPE).accept(MediaType.APPLICATION_JSON_TYPE).header("USER-AGENT", "my-java-sdk /1.1").method(methodName.toUpperCase(), String.class, f);

The result is the following exception:

 com.sun.jersey.api.client.ClientHandlerException: java.net.ProtocolException: Invalid HTTP method: FOOBAR

I have tried various ways to try and resolve this, without success.

  • http://java.net/jira/browse/JERSEY-639 has been implemented above in the config.getProperties() line. Still receiving the error
  • when i switch to the apache http client, i receive 411 errors from the server receiving the requests for all non-GET and non-PUT requests.

Long story short, I want to implement similar functionality as is available in via Java:

  • Consuming Custom Request Methods with Android
  • Consuming Custom Request Methods with iOS5

Thank you in advance for your feedback

like image 600
sdolgy Avatar asked May 18 '12 17:05

sdolgy


1 Answers

With Jersey 2.x Client, we would set the property

  • HttpUrlConnectorProvider.SET_METHOD_WORKAROUND

to true

Client client = ClientBuilder.newClient();
client.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true);
String response = client.target(url).request().method("PATCH", entity, String.class);
like image 192
Paul Samsotha Avatar answered Oct 12 '22 01:10

Paul Samsotha