user1354825
user1354825

Reputation: 1541

Passing special characters as Query parameters in a Spring RESTful application

I want to pass " & ; \n ? etc as characters in my query parameter. But since they have special meaning in HTTP protocol , i feel i need to somehow 'escape' these characters to disable their functionality.

How can i do that if i want to pass them as a normal string sequence in a RESTful Spring boot application query parameter ?

I was able to replace the \n with the %0A as some answers on here suggested , but i guess i need to do some more fixes. Here is my string :-

"package drl; %0A import com.example.demo.Apple; %0A dialect 'mvel' %0A rule Apple_is_green_yall %0A when %0A $person : Apple(color == \"Green\") %0A then %0A System.out.println(\"Appl green yo guys \") %0A end"

Thanks !

Upvotes: 2

Views: 9057

Answers (1)

Jakub S.
Jakub S.

Reputation: 628

The data should be URL Encoded. You can use java.net.URLEncoder to replace all special characters with the appropriate %XY representation.

(https://docs.oracle.com/javase/7/docs/api/java/net/URLEncoder.html)

Like this:

import java.net.URLEncoder
[...]
String input = "#your $data %here"
// You can use other charsets but UTF-8 is recommended
String encoded = URLEncoder.encode(input, StandardCharsets.UTF_8);

This example will take the string "#your $data %here" and output "%23your%20%24data%20%25here".

Upvotes: 3

Related Questions