0

I want to add newline characters in query parameters for a get method in my RESTful API.

This is my final GET URL in Postman:-

www.localhost:8091/customRule?someString="this is some string \\n on new line"

I get the exception:-

java.lang.IllegalArgumentException: Invalid character found in the request target [/customRule?someString=%22this%20is%20some%20string%20\\n%20on%20new%20line%22]. The valid characters are defined in RFC 7230 and RFC 3986

How can i fix that ?

Natsu
  • 243
  • 1
  • 7
user1354825
  • 358
  • 3
  • 16

1 Answers1

1

I have read a few things about this topic, but decided that there is not exactly the answer you are looking for. But something like this came to my mind. In Java, the new line character is '\n'.

We need to encode the new line character like here.

I tried with a little code like this:

@GetMapping(value = "/new")
public ResponseEntity<?> newLineCharacter(@RequestParam("newline") String newLine) {
    String s = new StringBuilder("xxx").append(newLine).append("yyy").toString();
    log.info(s);
    return ResponseEntity.ok().build();
}

When I trigger this url with the encoded new line character %0A as follows:

localhost:8080/a/new?newLine=%0A

I see an output like this in my logs:

xxx

yyy

But there seems to be an important point to note here, In Linux, Windows or Mac environments, the new line character is different.

You can see the details here.

Therefore, using the new line character as my query param did not seem to me a very good idea.

I hope this helps you.

Community
  • 1
  • 1
Fatih
  • 551
  • 3
  • 17