Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 720
Ross Avatar asked Oct 19 '08 22:10

Ross


People also ask

How do I set parameters in HttpClient?

We can add parameters using String name-value pairs, or utilize NameValuePairs class for that purpose. Similarly, UriBuilder can be used to add parameters to other HttpClient request methods.

Can HTTP POST have query parameters?

POST should not have query param. You can implement the service to honor the query param, but this is against REST spec.

CAN GET method have query parameters?

When the GET request method is used, if a client uses the HTTP protocol on a web server to request a certain resource, the client sends the server certain GET parameters through the requested URL. These parameters are pairs of names and their corresponding values, so-called name-value pairs.

What are query parameters in HTTP?

Query parameters are a defined set of parameters attached to the end of a url. They are extensions of the URL that are used to help define specific content or actions based on the data being passed. To append query params to the end of a URL, a '? ' Is added followed immediately by a query parameter.


2 Answers

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")  });  
like image 192
Ryan Guest Avatar answered Sep 28 '22 02:09

Ryan Guest


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.

like image 34
Steve Jones Avatar answered Sep 28 '22 00:09

Steve Jones