0

I am trying to map URL with pipe separated parameters and unable to map in controller class with @RequestMapping("/service").

Example : Request url : http://localhost:8080/service?name=raj|mahesh|akshay|abhi

It is accepting when I tried with one name i.e. raj but throwing an exception when I tried with multiple name with pipe deliminator.

@RestController
class Employee {
@RequestMapping("/service", , method = RequestMethod.GET)
public String getNameService(@RequestParam(value="name", required=false) String name) throws Exception {
      .....
     System.out.println(name);
  }
}

Exception : 
java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986

How to get all values of param name ?
DB12
  • 1
  • 1

1 Answers1

2

Trying out your code (after fixing the obvious mistakes) I'm faced with the following exception when trying out your parameters:

HTTP Status 400 – Bad Request

Type Exception Report

Message Invalid character found in the request target [/service?name=raj|mahesh|akshay|abhi]. The valid characters are defined in RFC 7230 and RFC 3986

Description The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).
...

Whats going on here is that the pipe character, "|", is not a valid character to use in a URL.
There is a certain set if characters that can be used "safely" in a URL. Letters in the english alphabet, number, &, #, / and a couple more are safe.
The rest need to be URL-encoded for applications and webservers to handle them correctly.

You can find some more information on this topic here What characters are valid in a URL?

In your case the pipe character maps to the string %7C.
So just switching all your pipes to %7C will make it work.

Try http://localhost:8080/service?name=raj%7Cmahesh%7Cakshay%7Cabhi

deRailed
  • 579
  • 1
  • 4
  • 9
  • Good explanation, in my case I resolved enconding url before to send a request to the remote server. – Caio May 25 '21 at 21:47