Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle requests that includes forward slashes (/)?

I need to handle requests as following:

www.example.com/show/abcd/efg?name=alex&family=moore   (does not work) www.example.com/show/abcdefg?name=alex&family=moore   (works) www.example.com/show/abcd-efg?name=alex&family=moore   (works) 

It should accept any sort of character from the value that is located between www.example.com/show/ and ?. Please note the value that would be located there would be a single value not name of an action.

For example: /show/abcd/efg and /show/lkikf?name=Jack in which the first request should redirect user to the page abcd/efg (because thats a name) and the second one should redirect user to the page lkikf along with value of parameter name.

I have following controller to handle it but the issue is when I have / in the address the controller is unable to handle it.

@RequestMapping(value = "/{mystring:.*}", method = RequestMethod.GET) public String handleReqShow(             @PathVariable String mystring,             @RequestParam(required = false) String name,             @RequestParam(required = false) String family, Model model)     { 

I used following regex which did not work.

 /^[ A-Za-z0-9_@./#&+-]*$/ 
like image 490
Jack Avatar asked Jul 15 '15 03:07

Jack


People also ask

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.

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 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" .


1 Answers

Another way I do is:

@RequestMapping(value = "test_handler/**", method = RequestMethod.GET) 

...and your test handler can be "/test_hanlder/a/b/c" and you will get the whole value using following mechanism.

requestedUri = (String)  request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE); 
like image 193
Ayan Chakraborty Avatar answered Sep 20 '22 11:09

Ayan Chakraborty