Invoking a 'REST' service which have query parameters in the URL

15,442

Specify the parameters using the query method:

String path = "rest.asp";
webClient.path(path)
    .type(MediaType.APPLICATION_JSON)
    .accept(MediaType.TEXT_XML_TYPE)
    .query("param1","34")
    .query("param2","88")
    .query("param3","foo")
    .get(Response.class);
Share:
15,442
TJ-
Author by

TJ-

--o--O.O--o--

Updated on June 21, 2022

Comments

  • TJ-
    TJ- almost 2 years

    I have to invoke a GET on a service which returns text/xml.

    The endpoint is something like this:

    http://service.com/rest.asp?param1=34&param2=88&param3=foo
    

    When I hit this url directly on a browser (or some UI tool), all's good. I get a response.

    Now, I am trying to use CXF WebClient to fetch the result using a piece of code like this:

    String path = "rest.asp?param1=34&param2=88&param3=foo";
    
    webClient.path(path)
        .type(MediaType.APPLICATION_JSON)
        .accept(MediaType.TEXT_XML_TYPE)
        .get(Response.class);
    

    I was debugging the code and found that the request being sent was url encoded which appears something like this:

    http://service.com/rest.asp%3Fparam1=34%26param2=88%26param3=foo
    

    Now, the problem is the server doesn't seem to understand this request with encoded stuff. It throws a 404. Hitting this encoded url on the browser also results in a 404.

    What should I do to be able to get a response successfully (or not let the WebClient encode the url)?