26

I am trying to send an HTTP GET with a json object in its body. Is there a way to set the body of an HttpClient HttpGet? I am looking for the equivalent of HttpPost#setEntity.

Code-Apprentice
  • 69,701
  • 17
  • 115
  • 226
Scott Swank
  • 614
  • 1
  • 6
  • 7

4 Answers4

38

From what I know, you can't do this with the default HttpGet class that comes with the Apache library. However, you can subclass the HttpEntityEnclosingRequestBase entity and set the method to GET. I haven't tested this, but I think the following example might be what you're looking for:

import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;

public class HttpGetWithEntity extends HttpEntityEnclosingRequestBase {
    public final static String METHOD_NAME = "GET";

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

Edit:

You could then do the following:

...
HttpGetWithEntity e = new HttpGetWithEntity();
...
e.setEntity(yourEntity);
...
response = httpclient.execute(e);
Nayan
  • 1,362
  • 2
  • 12
  • 25
torbinsky
  • 1,330
  • 9
  • 17
  • Seems to work, that's actually the only way I've found so far to send a body using a get request with any java library! – javanna Nov 22 '13 at 18:11
  • +1; @ScottSwank please accept the answer as it does indeed seem to work – mkl Jan 10 '14 at 08:48
6

Using torbinsky's answer I created the above class. This lets me use the same methods for HttpPost.

import java.net.URI;

import org.apache.http.client.methods.HttpPost;

public class HttpGetWithEntity extends HttpPost {

    public final static String METHOD_NAME = "GET";

    public HttpGetWithEntity(URI url) {
        super(url);
    }

    public HttpGetWithEntity(String url) {
        super(url);
    }

    @Override
    public String getMethod() {
        return METHOD_NAME;
    }
}
icapurro
  • 198
  • 2
  • 8
  • I tried the above code with a web service that I need to talk to on my project and no go. I just receive on the service daemon log "Internal application error, closing connection.". Jersey offered a HttpGet with entities (called differently), but sadly apache does not. This particular web service truly expects a GET request and not a POST. – Sarah Weinberger Jan 15 '15 at 16:17
1

How we can send request uri in this example just like HttpGet & HttpPost ???

 public class HttpGetWithEntity extends HttpEntityEnclosingRequestBase
 {
    public final static String METHOD_NAME = "GET";
    @Override
     public String getMethod() {
         return METHOD_NAME;
     } 

        HttpGetWithEntity e = new HttpGetWithEntity(); 
        e.setEntity(yourEntity); 
        response = httpclient.execute(e); 
}
0

In addition torbinsky's answer, you can add these constructors to the class to make it easier to set the uri:

public HttpGetWithEntity(String uri) throws URISyntaxException{
    this.setURI(new URI(uri));
}

public HttpGetWithEntity(URI uri){
    this.setURI(uri);
}

The setURI method is inherited from HttpEntityEnclosingRequestBase and can also be used outside the constructor.