30

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
  • 81,660
  • 23
  • 145
  • 268
Scott Swank
  • 664
  • 1
  • 6
  • 7

6 Answers6

46

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,521
  • 2
  • 13
  • 27
torbinsky
  • 1,450
  • 10
  • 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
10

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
  • 348
  • 3
  • 9
  • 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
4

My approach is to use a RequestBuilder (from org.apache.http.client.methods package).

It doesn't conform the same API as just using HttpPost but is much simpler than the one provided above.

HttpUriRequest request = RequestBuilder.get(uri)
        .setEntity(new StringEntity(entity))
        .setHeader(HttpHeaders.CONTENT_TYPE, "application/json")
        .build();
Benjamin
  • 3,217
  • 2
  • 27
  • 42
2

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.

0

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
import java.net.URI;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;

public class HttpGetWithEntity extends HttpEntityEnclosingRequestBase {

    public final static String METHOD_NAME = "GET";

    public HttpGetWithEntity() {
        super();
    }

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

    public HttpGetWithEntity(final String url) {
        super();
        setURI(URI.create(url));
    }

    @Override
    public String getMethod() {
        return METHOD_NAME;
    }
}
ez2sarang
  • 106
  • 1
  • 4