Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 952
dukable Avatar asked May 15 '12 13:05

dukable


People also ask

How do you pass a parameter with space in REST API?

Rest API Request with parameter value containing spaces must be filled with %20.

How do you put a space in REST API URL?

Our recommendation is to avoid using spaces in URLs, and instead use hyphens to separate words. If you are unable to do this, make sure to encode whitespace using "+" or "%20" in the query-string, and using "%20" within the rest of the URL.

How do I send a space in an HTTP request?

If you look at RFC 3986 Appendix A, you will see that "space" is simply not mentioned anywhere in the grammar for defining a URL. Since it's not mentioned anywhere in the grammar, the only way to encode a space is with percent-encoding ( %20 ).


1 Answers

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

like image 186
alexey28 Avatar answered Oct 17 '22 11:10

alexey28