TJ-
TJ-

Reputation: 14363

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

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)?

Upvotes: 7

Views: 16219

Answers (2)

Andre
Andre

Reputation: 691

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);

Upvotes: 10

Romin
Romin

Reputation: 8806

You will need to encode your URL. You can do it with the URLEncoder class as shown below:

Please replace your line

String path = "rest.asp?param1=34&param2=88&param3=foo";

with

String path = URLEncoder.encode("rest.asp?param1=34&param2=88&param3=foo");

Upvotes: 0

Related Questions