user5746191
user5746191

Reputation:

Sending "?" and "=" characteres in the query string with RESTEasy

I am creating REST API in my application with RESTEasy:

@GET
@Path("/api")
public Response getData(@QueryParam("from") String from, 
                        @Context HttpServletRequest httpRequest)
                        throws ProtocolException, MalformedURLException, IOException {

    System.out.println("from: " + from);

    ...
}

This code belongs to give data for following API:

http://localhost:8080/LynkBeta/f/query?from=search?q=developer

I am getting only the following value from the query parameter when I print it:

search?q 

But the actual value is:

search?q=developer

The query parameter contains an equal to symbol (=). It works fine without it. I know that definitely I am missing something here.

How to handle this kind of values? What I need to include to handle this?

I researched bit, but there is no straight forward answer for my search.

Upvotes: 3

Views: 5527

Answers (1)

cassiomolin
cassiomolin

Reputation: 130877

Short answer

To send search?q=developer as a value of a query parameter, this value must be URL encoded. So, search?q=developer will become search%3Fq%3Ddeveloper.

Query string

According to the RFC 3986, the query string is defined as following:

3.4. Query

The query component contains non-hierarchical data that, along with data in the path component, serves to identify a resource within the scope of the URI's scheme and naming authority (if any). The query component is indicated by the first question mark ("?") character and terminated by a number sign ("#") character or by the end of the URI. [...]

Characters such as ? (used to start the query string), & (used to separate the query parameters) and = (used to associate the parameter with its value) are reserved.

If you need to send such characteres as values of query string parameters, they need to be URL encoded. For more details on when the encoding is necessary, check the section 2.4 When to Encode or Decode of the RFC 3986.

URL Encoding

URL encoding is also known as percent enconding:

2.1. Percent-Encoding

A percent-encoding mechanism is used to represent a data octet in a component when that octet's corresponding character is outside the allowed set or is being used as a delimiter of, or within, the component. A percent-encoded octet is encoded as a character triplet, consisting of the percent character "%" followed by the two hexadecimal digits representing that octet's numeric value. For example, "%20" is the percent-encoding for the binary octet "00100000" (ABNF: %x20), which in US-ASCII corresponds to the space character (SP). [...]

And many languages support it. Here are examples in Java and JavaScript:

String param = "search?q=developer";
String url = "http://localhost:8080/LynkBeta/f/query?from=" + 
    URLEncoder.encode(param, "UTF-8");
var param = "search?q=developer";
var url = "http://localhost:8080/LynkBeta/f/query?from=" + 
    encodeURIComponent(param);

Encoded parameters with JAX-RS

Consider, for example, you are requesting http://example.com/api/query?biz=search%3Fq%3Ddeveloper.

By default, when using the @QueryParam annotation, JAX-RS will URL decode query parameters:

@GET
@Path("/foo")
public Response someMethod(@QueryParam("biz") String biz) {
    ...
}

So, the value of the biz parameter will be search?q=developer.

If you need to receive the parameters URL encoded, used the @Encoded annotation:

@GET
@Path("/foo")
public Response someMethod(@QueryParam("biz") @Encoded String biz) {
    ...
}

Now, the value of the biz parameter will be search%3Fq%3Ddeveloper.

Upvotes: 4

Related Questions