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.
Asked
Active
Viewed 2.1k times
26
-
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
4 Answers
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);
-
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
-
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);
}
user2389517
- 21
- 4
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.
Brandon Lyday
- 1
- 1