Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between `removeQuery()` and `clearParameters()` in org.apache.http.client.utils.URIBuilder of HttpClient 4.3.1?

It seems to me that these two methods (URIBuilder.removeQuery and URIBuilder.clearParameters) do exactly the same thing, so I am not sure why there are two choices. In what circumstances should I use one over the other?

I noticed that the corresponding URIBuilder.setQuery method is marked as deprecated in favor of URIBuilder.setParameters, but URIBuilder.removeQuery is not. Am I mistaken in thinking that perhaps it should be?

UPDATE: Oleg offered the following explanation on the dev mailing list:

The reason for deprecation of the #setQuery method was its inconsistency of its contract with all other methods of the class. #setQuery expected input to be URL encoded whereas all other methods expect unescaped input. The choice was between changing the contract of the method and by doing so breaking pretty much every single application reliant on URIBuilder or method deprecation in favor of another method with slightly less intuitive name. So, one should be using [set|clear| add]Parameter[s] methods to work with query parameters and the #setCustomQueury method to set custom queries.

like image 244
jacobq Avatar asked Dec 02 '25 11:12

jacobq


1 Answers

Well, exact difference is very small, if you look into the source :

public URIBuilder removeQuery() {
    this.queryParams = null;
    this.query = null; // <- this is the only difference
    this.encodedQuery = null;
    this.encodedSchemeSpecificPart = null;
    return this;
}

vs

public URIBuilder clearParameters() {
    this.queryParams = null;
    this.encodedQuery = null;
    this.encodedSchemeSpecificPart = null;
    return this;
}

So, clearParameters will keep your query object.

like image 126
kiruwka Avatar answered Dec 05 '25 02:12

kiruwka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!