In Sending Https Request Using Javax Webtarget Client, How to Avoid Encoding of Query Params That Contain ": ["?

I am using WebTarget to send a GET request with query params.

This is my expected request that I need to generate:

{{host}}/svc/lookupservice/v2/findItems?filter=creationdate:[2020-05-01T00:08:17.000Z..]&limit=25&offset=0&userType=seller&userid=11234567

But using webtarget as follows:

import javax.ws.rs.client.Client;
...
...
class A {
    private Client client;

  public void apiCall(String userId, String startDate, String endDate) {
    String endpoint = (String)this.client.getConfiguration().getProperty("jaxrs.client.endpointuri");
    WebTarget webTarget = this.client.target(endpoint).path("/lookupservice/v2/findItems")
              .queryParam("userType", new Object[]{"seller"})
              .queryParam("sort", new Object[]{"creationdate"})
              .queryParam("limit", new Object[]{150})
              .queryParam("offset", new Object[]{0})
              .queryParam("filter", new Object[]{"creationdate:[" + startDate + ".." + endDate + "]"})
              .queryParam("userid", new Object[]{userId});

...
}

...
}

class B {

   main(..) {
      Date endDate = new Date();
      Date startDate = new Date(endDate.getTime() - Duration.ofDays(30).toMillis());
      SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault());

      sDate = formatter.format(startDate);
      eDate = formatter.format(endDate);

      new A().apiCall(userId, sDate, eDate);

  }
      

}

And this above generates webTarget url as

JerseyWebTarget {  }

It encodes :[ in creation date.

How can I avoid it?

1 Answer

Figured out by not using webTarget's .queryParams() and instead passing URI with query.

String query = "?userType=seller&limit=25&offset=0&userid=" + userId + "&filter=creationdate:[" + startDate + ".." + endDate + "]";

URI uri = URI.create(endpoint + "/lookupservice/v2/findItems"+ query);

WebTarget webTarget = client.target(uri);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest