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.
- 
                    Almost duplicate: http://stackoverflow.com/questions/978061/http-get-with-request-body – Mikita Belahlazau Sep 21 '12 at 17:10
- 
                    Nikita - My question is similar to the one you post, however I am asking specifically how to do so via the Apache HttpClient API. Thank you though. – Scott Swank Sep 21 '12 at 17:14
- 
                    The answer is oficially it's not possible. GET requests can't contain body according to specification. You need to use POST requests. – Mikita Belahlazau Sep 21 '12 at 17:15
- 
                    1Please note that in the question you linked to it is indicated that a GET may have a body. The spec allows this. My question is simply whether this is possible via Apache's HttpClient. – Scott Swank Sep 21 '12 at 17:19
- 
                    Yes, you're right it's not prohibited by spec. But I'd say it's not possible. – Mikita Belahlazau Sep 21 '12 at 17:27
6 Answers
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);
- 
                    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
- 
                    
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;
    }
}
 
    
    - 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
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();
 
    
    - 3,217
- 2
- 27
- 42
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.
 
    
    - 21
- 2
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); 
}
 
    
    - 19
- 4
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;
    }
}
 
    
    - 106
- 1
- 4
- 
                    I found @NotThreadSafe not found error https://stackoverflow.com/questions/57472392 – ez2sarang Apr 26 '22 at 02:10
 
     
     
    