8

Boss wants us to send a HTTP GET with parameters in the body. I can't figure out how to do this using org.apache.commons.httpclient.methods.GetMethod or java.net.HttpURLConnection;.

GetMethod doesn't seem to take any parameters, and I'm not sure how to use HttpURLConnection for this.

MedicineMan
  • 15,008
  • 32
  • 101
  • 146

3 Answers3

4

You can extends the HttpEntityEnclosingRequestBase class to override the inherited org.apache.http.client.methods.HttpRequestBase.getMethod() but by fact HTTP GET does not support body request and maybe you will experience trouble with some HTTP servers, use at your own risk :)

public class MyHttpGetWithEntity extends HttpEntityEnclosingRequestBase {
    public final static String GET_METHOD = "GET";
    
    public MyHttpGetWithEntity(final URI uri) {
        super();
        setURI(uri);
    }
    
    public MyHttpGetWithEntity(final String uri) {
        super();
        setURI(URI.create(uri));
    }

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

then


    import org.apache.commons.io.IOUtils;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.DefaultHttpClient;
    
    public class HttpEntityGet {
    
        public static void main(String[] args) {
            
            try {
                HttpClient client = new DefaultHttpClient();
                MyHttpGetWithEntity e = new MyHttpGetWithEntity("http://....");
                e.setEntity(new StringEntity("mystringentity"));
                HttpResponse response = client.execute(e);
                System.out.println(IOUtils.toString(response.getEntity().getContent()));
            } catch (Exception e) {
                System.err.println(e);
            }
        }
    } 

  
        
Anonymous Duck
  • 2,942
  • 1
  • 12
  • 35
vzamanillo
  • 9,905
  • 1
  • 36
  • 56
  • I found this worked nicely, but unfortunately I had to bin my earlier Jersey implementation in favour of HttpClient (I don't have an issue with HttpClient, but it was a bit of a pain to have gone halfway down the Jersey route and to then backtrack) – Brian Agnew Mar 08 '22 at 16:57
3

HTTP GET method should NEVER have a body section. You can pass your parameters using URL query string or HTTP headers.

If you want to have a BODY section. Use POST or other methods.

LHA
  • 9,398
  • 8
  • 46
  • 85
  • 4
    Posted before I was finished :|. The HTTP spec says that a GET can have a body, but an HTTP server is not required to read it. – Sotirios Delimanolis Dec 12 '13 at 20:01
  • For now, just other SO questions: http://stackoverflow.com/questions/978061/http-get-with-request-body – Sotirios Delimanolis Dec 12 '13 at 20:11
  • Nono, I mean the HTTP specification doesn't say anything against putting content in the body of an HTTP request. Take a look at [Dave Durbin's answer](http://stackoverflow.com/a/15656884/438154). The server is of no importance. – Sotirios Delimanolis Dec 12 '13 at 20:21
  • Even HTTP spec does not mention about it. If you program to send body along with GET, does standard server like Tomcat, jBoss, IIS understand the body? and parse the body? – LHA Dec 12 '13 at 20:22
  • That's what I mean. It doesn't have to, but the Spec doesn't prevent you from sending it. – Sotirios Delimanolis Dec 12 '13 at 20:23
  • Yes. so what is why I say I am talking about standard http servers and you talking about non-standard http servers. Anyway. Thank you for your notes. I didn't know Http GET can have body before. :) – LHA Dec 12 '13 at 20:25
  • For a server to be called an HTTP server, it has to comply with the HTTP specification. The spec says that a resource requested through a GET can be identified with the Request-Uri without ever needing to check the request body. If the server _does_ check the request body, that doesn't make it any less of a compliant server. – Sotirios Delimanolis Dec 12 '13 at 20:29
  • Absolutely agree with you. – LHA Dec 12 '13 at 20:30
  • 2
    This is old and outdated badly, new spec is fine with GET having a body. What if the query string is way too long? Are you going to POST/PUT instead when its not creating/updating anything? 2014 Spec: A payload within a GET request message has no defined semantics; sending a payload body on a GET request might cause some existing implementations to reject the request. – Jan Aug 24 '21 at 06:01
  • I've downvoted this. You can say that a GET method should never have a body, but that doesn't stop people implementing this (ElasticSearch - I'm looking at you!) and so a means of querying these services is required – Brian Agnew Mar 08 '22 at 13:38
0

Using java.net.http.HttpRequest, when building one with a RequestBuilder instead of using the .GET() method that doesn't take paramenters, user the .method(method, bodyPublisher)

HttpClient clientFwd = HttpClient.newHttpClient();
HttpRequest get = HttpRequest.newBuilder()
            .uri(URI.create(YOURURI))
            //GET, POST, PUT,..
            .method("GET", HttpRequest.BodyPublishers.ofString(bodyGet))
            .header("Content-Type", "application/json")
            .header("Authorization", "Bearer "+TOKEN)
            .build();

Then get.send() your HttpRequest to get your response.

DarkCygnus
  • 7,420
  • 4
  • 36
  • 59