Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass string path param containing slash character?

I have this REST resource:

@GET
@Path("{business},{year},{sample}")
@Produces(MediaType.APPLICATION_JSON)
public Response getSample(
        @PathParam("business") String business,
        @PathParam("year") String year,
        @PathParam("sample") String sampleId {
    Sample sample = dao.findSample(business, year, sampleId);
    return Response.ok(sample).build();
}

sample param can contain slash character: 6576/M982, for instance.

I'm calling it with http://ip:port/samples/2000,2006,6576/M982 but does not work, obviously.

I have also tried with http://ip:port/samples/2000,2006,6576%2FM982, encoding the slash as %2F, but doesn't work either, it doesn't reach the endpoint.

EDIT

I'm using Retrofit to call the endpoint and I do this:

@GET("/samples/{business},{year},{sampleId}")
Observable<Sample> getSampleById(
        @Path("business") String business,
        @Path("year") String year,
        @Path(value = "sampleId", encoded = true) String sampleId);

With encoded = true, but still not working.

like image 341
Héctor Avatar asked Jul 18 '16 11:07

Héctor


People also ask

How do you pass a slash through a string?

Solution 1. The backslash ( "\" ) character is a special escape character used to indicate other special characters such as new lines ( \n ), tabs ( \t ), or quotation marks ( \" ). If you want to include a backslash character itself, you need two backslashes or use the @ verbatim string: "\\Tasks" or @"\Tasks" .

Can path variable contain slash?

Unfortunately, we soon find out that this returns a 404 if the PathVariable contains a slash. The slash character is the URI standard path delimiter, and all that goes after it counts as a new level in the path hierarchy. As expected, Spring follows this standard.

How do you handle a slash in a URL?

The addition of a slash at the end of a URL instructs the web server to search for a directory. This speeds the web page loading because the server will retrieve the content of the web page without wasting time searching for the file.

How do you escape a slash in path?

About Escaping File Paths Characters to be escaped include the backslash (\, because it is the escaping character) and the double quotes ("). Both of these characters can be relevant in file paths.


2 Answers

Try to use {sample:.+} instead of {sample}

The @Path annotation is a regular expression and regex do not match / character.

To override the regex, we can add .+ at the end of the PathParam.

In this way we can allow / in our path and avoid using %2F.

like image 37
steva1988 Avatar answered Nov 01 '22 17:11

steva1988


Reserved characters such as , and / must be URL encoded.

  • , is encoded as %2C
  • / is encoded as %2F

Try http://ip:port/samples/2000%2C2006%2C6576%2FM982.


The RFC 3986 defines the following set of reserved characters that can be used as delimiters. Hence, they require URL encoding:

: / ? # / [ ] / @ ! $ & ' ( ) * + , ; =

Unreserved characters do not require URL encoding:

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
a b c d e f g h i j k l m n o p q r s t u v w x y z
0 1 2 3 4 5 6 7 8 9 - _ . ~

If URL encoding , is not a good alternative for you, you could consider using query parameters. Your code will be like:

@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getSample(@QueryParam("business") String business, 
                          @QueryParam("year") String year,
                          @QueryParam("sample") String sampleId {
    ...
}

And your URL will be like http://ip:port/samples?business=2000&year=2006&sample=6576%2FM982.

Please note that the / still needs to be URL encoded.

like image 155
cassiomolin Avatar answered Nov 01 '22 18:11

cassiomolin