0

I'm trying to pass some XML into a URL for a service I'm calling, but when I run this, it's giving me an IllegalArgumentException

response = Unirest.post(appSettings.getURL() + "&service=test&xml=<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><test><cid>blah</cid><pw>blah</pw></test>")

Here's the error:

java.lang.IllegalArgumentException: Illegal character in query at index 108: http://test&service=test&xml=%3C?xml version="1.0" encoding="UTF-8" standalone="no"?><test><cid>blah</cid><pw>blah</pw></test>

I think it has something to do with how the XML is getting read in, but I'm having trouble figuring out exactly what.

Spance
  • 555
  • 3
  • 11
  • 29
  • 2
    Why? You're sending a post requests. Post requests have a body, which can contain anything you want. Why do you pass xml as a URL parameter? And without urlencoding it, which is even worse? – JB Nizet Sep 27 '17 at 16:21
  • Is the ? sign in the original header meant to be there? – Assafs Sep 27 '17 at 16:21
  • unfortunately the service I'm calling requires the xml to be a parameter – Spance Sep 27 '17 at 16:27

1 Answers1

3

Spaces are escaped in the URL as %20 and other non alphanumeric characters can be problematic. Try to use UrlEncoder http://docs.oracle.com/javase/8/docs/api/java/net/URLEncoder.html to encode URL parameters:

Unirest.post(appSettings.getURL() + "&service=test&xml=" + URLEncoder.encode("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><test><cid>blah</cid><pw>blah</pw></test>", "UTF-8"))

You can also try URIBuilder https://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/utils/URIBuilder.html or other methods Java URL encoding of query string parameters

The XML has slash characters and question mark which are natural part of the URL parameter syntax, but supposedly that's not a problem. Since we are talking about REST, couldn't you pass the XML information along as part of the JSON payload for the request (or response)?

For generic (even binary) URL parameters one hack I can image is to Base64 encode the payload you want to pass (in this case XML), put that in the URL, and on the other end you can Base64 decode it.

Also keep in mind that for security reasons (to block possible web related exploits which often manipulate with huge URLs) browsers, HTTP servers, and frameworks enforce maximum length for the URL. It's in the ballpark of 1-2 kilobytes, so you don't have too much space for XML data.

Csaba Toth
  • 10,021
  • 5
  • 75
  • 121
  • 1
    the URLEncoder does the trick. I do need to keep the XML in the URL, and I also have no control on the other end of this particular service – Spance Sep 27 '17 at 16:31
  • The slashes and question marks are not a problem. They will be encoded, too. Just as the rest of the special characters. – JB Nizet Sep 27 '17 at 16:32
  • @JBNizet As you read it yes. On the other end, if some part of the framework tries to be smart and temporarily surprise decode it... there's a lot going on when interpreting URLs – Csaba Toth Sep 27 '17 at 16:40