Search code examples
javajax-rsspecial-charactersurlencodejava-ee-7

How add special characters in WebTarget.queryParam name in client JavaEE 7


I try make request using client library javax.ws.rs

I'm trying to add a parameter name containing characters [ and ] to query parameters, for encdoded special characters i am used URLEncoder.encode(). But after the request, the response contains data without this parameter, the server ignored this request parameter. I made a request on the command line using "curl" on purpose with an error in this parameter and got the same result as from the request in the application. The error is clearly in the encoded parameter name, but I don’t understand how to correctly add parameters containing special characters.

The Code:

    WebTarget webTarget = new Client().target(uri);
    String key = "filters[Time].Start";
    key = URLEncoder.encode(key,StandardCharsets.UTF_8.toString());

    String value = "2022-08-27+17:00:00";
    value = URLEncoder.encode(value,StandardCharsets.UTF_8.toString());
    System.out.println(key + " " + value);
    webTarget = webTarget.queryParam("per_page","10");
    webTarget = webTarget.queryParam(key,value);
    webTarget = webTarget.queryParam("order_by","time");

    invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
    Response response = invocationBuilder.get();

Result sout : filters%5BTime%5D.Start 2022-08-27%2B17%3A00%3A00


Solution

  • By advise @cyberbrain, i did checked my request in server by help 'tcpdump' utilite with key '-A'.

    I did request by curl and after by my java application and compare data.

    In my case problems was in value parametrs, my value equels "2022-08-27+17:00:00". After add in queryparametr this symbol '+' encoded to code '%2B' and this don't like my server. I just replaced symbol '+' to symbol space (code %20) in value ("2022-08-27 17:00:00"). After that i geted correct data from my server