How to pass spaces in a REST call using HttpGet in java

dukable picture dukable · May 15, 2012 · Viewed 25.6k times · Source

I am sending a call to a SMS gateway using their REST API. Everything is fine when I send a simple word like 'Hello', but if I add a space then I got in trouble. This because the URI cannot contain spaces.

What is the proper way to do what I need to do?

HttpClient httpclient = new DefaultHttpClient();
try {
    HttpGet httpget = new HttpGet("http://www.example.com/SecureREST/SimpleSMSsend?PhoneNumber=123&Message=hello how are you?");
httpget.addHeader(new BasicHeader("Accept", "application/json"));

// Create a response handler
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httpget, responseHandler);
System.out.println("----------------------------------------");
System.out.println(responseBody);
System.out.println("----------------------------------------");
} finally {
httpclient.getConnectionManager().shutdown();
}

Resulting on the IllegalArgumentException:

Exception in thread "main" java.lang.IllegalArgumentException
at java.net.URI.create(Unknown Source)
at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:69)
at main.main(main.java:36)
Caused by: java.net.URISyntaxException: Illegal character in query at index 97: https://www.example.com/SecureREST/SimpleSMSsend?PhoneNumber=123&Message=Hello, how are you?
at java.net.URI$Parser.fail(Unknown Source)
at java.net.URI$Parser.checkChars(Unknown Source)
at java.net.URI$Parser.parseHierarchical(Unknown Source)
at java.net.URI$Parser.parse(Unknown Source)
at java.net.URI.<init>(Unknown Source)
... 3 more

Edit: As suggested by alexey28 I am using the Encoder, here is what I do now:

String query = "?PhoneNumber=123&Message=Hello, how are you?";
String host = "https://www.example.com/SecureREST/SimpleSMSsend";
String encodedUrl = host + URLEncoder.encode(query,"utf-8");
HttpGet httpget = new HttpGet(encodedUrl);

But is results in

Exception in thread "main" org.apache.http.client.HttpResponseException: **Bad Request**
at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:67)
at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:54)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:735)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:709)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:700)
at main.main(main.java:47)

What am I doing wrong here?

The request once encoded: executing request https://www.example.com/SecureREST/SimpleSMSsend%3FPhoneNumber%3D123%26Message%3DHello%2C+how+are+you%3F

Answer

alexey28 picture alexey28 · May 15, 2012

Before sending use URLEncoder to encode URL parameters values:

String restUrl = URLEncoder.encode("You url parameter value", "UTF-8");

It will replace all your symbols including spaces -> '+' with proper one for URL