2

We are currently using HttpClient 4.3 for all REST communications with other service providers, however, one of our recent providers has some custom methods sitting within the API.

I was trying to figure out a way to send custom methods (Methods not in standard such as GET, POST, PUT, etc.).

Anyone has similar experience before by using HttpClient? Thanks!

0

2 Answers 2

7

I suggest that you use the RequestBuilder class (javadoc) to construct the Request objects. The static RequestBuilder.create method allows you to specify any "method" String for your request object.

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

Comments

2

Implement FOO method by extending HttpRequestBase as follows:

public class HttpFoo extends HttpRequestBase{

    public final static String METHOD_NAME = "FOO";

    public HttpFoo() {
        super();
    }

    @Override
    public String getMethod() {
        return METHOD_NAME;  
    }

    public HttpFoo(final String uri) {
        super();
        setURI(URI.create(uri));
    }

    public String getName() {
        return "FOO";
    }
}

And then the above method can be used just like any other existing methods like GET(HttpGet) etc.

CloseableHttpClient httpClient = HttpClients.createDefault();
HttpFoo fooMethod = new HttpFoo(fooUrl);        
HttpResponse response = httpClient.execute(fooMethod);

Have tested with

  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpclient</artifactId>
  <version>4.5.2</version>

Comments

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.