How do I add query parameters to a GetMethod (using Java commons-httpclient)?
Asked Answered
M

3

33

Using Apache's commons-httpclient for Java, what's the best way to add query parameters to a GetMethod instance? If I'm using PostMethod, it's very straightforward:

PostMethod method = new PostMethod();
method.addParameter("key", "value");

GetMethod doesn't have an "addParameter" method, though. I've discovered that this works:

GetMethod method = new GetMethod("http://www.example.com/page");
method.setQueryString(new NameValuePair[] {
    new NameValuePair("key", "value")
});

However, most of the examples I've seen either hard-code the parameters directly into the URL, e.g.:

GetMethod method = new GetMethod("http://www.example.com/page?key=value");

or hard-code the query string, e.g.:

GetMethod method = new GetMethod("http://www.example.com/page");
method.setQueryString("?key=value");

Is one of these patterns to be preferred? And why the API discrepancy between PostMethod and GetMethod? And what are all those other HttpMethodParams methods intended to be used for?

Middleoftheroader answered 19/10, 2008 at 22:31 Comment(0)
C
22

Post methods have post parameters, but get methods do not.

Query parameters are embedded in the URL. The current version of HttpClient accepts a string in the constructor. If you wanted to add the key, value pair above, you could use:

String url = "http://www.example.com/page?key=value";
GetMethod method = new GetMethod(url);

A good starting tutorial can be found on the Apache Jakarta Commons page.

Update: As suggested in the comment, NameValuePair works.

GetMethod method = new GetMethod("example.com/page"); 
method.setQueryString(new NameValuePair[] { 
    new NameValuePair("key", "value") 
}); 
Courcy answered 19/10, 2008 at 22:49 Comment(2)
I discovered that this works: GetMethod method = new GetMethod("example.com/page"); method.setQueryString(new NameValuePair[] { new NameValuePair("key", "value") }); This isn't mentioned on the tutorial page, however. Should this pattern be avoided?Middleoftheroader
Hm, apparently you can't put code blocks in comments, so I've edited my question to add that example and others.Middleoftheroader
H
18

It's not just a matter of personal preference. The pertinent issue here is URL-encoding your parameter values, so that the values won't get corrupted or misinterpreted as extra delimiters, etc.

As always, it is best to read the API documentation in detail: HttpClient API Documentation

Reading this, you can see that setQueryString(String) will NOT URL-encode or delimit your parameters & values, whereas setQueryString(NameValuePair[]) will automatically URL-encode and delimit your parameter names and values. This is the best method whenever you are using dynamic data, because it might contain ampersands, equal signs, etc.

Hypomania answered 9/8, 2010 at 19:8 Comment(0)
B
10

Try it this way:

    URIBuilder builder = new URIBuilder("https://graph.facebook.com/oauth/access_token")
            .addParameter("client_id", application.getKey())
            .addParameter("client_secret", application.getSecret())
            .addParameter("redirect_uri", callbackURL)
            .addParameter("code", code);

    HttpPost method = new HttpPost(builder.build());
Bissonnette answered 23/10, 2012 at 15:10 Comment(4)
what package is UrlBuilder in?Karrikarrie
I think it should be: "GetMethod(builder.build())"Excrement
URIBuilder can be found at: org.apache.http.client.utils.URIBuilder;Pals
URIBuilder is Apache HttpClient 4.5.x onwards. Older versions are out of luck and may have to rely on method.setQueryString(new nameValuePair[]{ new NameValuePair("theKey", "theValue" });Ferula

© 2022 - 2024 — McMap. All rights reserved.