【问题标题】:Newline characters in String query parameter字符串查询参数中的换行符
【发布时间】:2020-06-06 19:47:20
【问题描述】:

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

这是我在 Postman 中的最终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】:

我已阅读有关此主题的一些内容,但确定不完全是您正在寻找的答案。但是我想到了这样的事情。 在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 触发此网址时,如下所示:

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

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

xxx

年年

但这里似乎有一个重要的点要注意, 在LinuxWindowsMac 环境中,换行符不同

您可以查看详情here

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

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-09
    相关资源
    最近更新 更多