字符串查询参数中的换行符

问题描述 投票:0回答:1

我想在我的RESTful API中为get方法的查询参数中添加换行符。

这是我在邮递员中的最终GET URL:-

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

我得到例外:-

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

我该如何解决?

spring-boot restful-url
1个回答
0
投票

我已经阅读了有关此主题的一些内容,但认为您所要查找的答案不完全正确。但是我想到了这样的事情。在Java中,新行字符为'\n'。

我们需要对换行符like here进行编码。

我尝试了一些像这样的小代码:

@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();
}

当我使用如下编码的换行符%0A触发此url时:

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

我在日志中看到这样的输出:

xxx

yyy

但是这里似乎有一个重要的要注意的地方,在LinuxWindowsMac环境中,换行符为different

您可以看到详细信息here

因此,在我看来,将换行符用作查询参数不是一个好主意。

希望对您有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.