7

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

Jonathan
  • 20,053
  • 6
  • 63
  • 70
TJ-
  • 14,085
  • 12
  • 59
  • 90

2 Answers2

10

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);
Andre
  • 691
  • 3
  • 11
  • Thanks Andre. This is exactly what I needed. I looked at and intended to use .query but misinterpreted the documentation. – TJ- Feb 08 '13 at 11:20
0

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");
Romin
  • 8,708
  • 2
  • 24
  • 28