【随手记录】关于Invalid character found in the request target.The valid characters are defined in RFC 7230 and RFC3986

 

今天调用第三方的接口时候遇到一个错误:

Invalid character found in the request target.The valid characters are defined in RFC 7230 and RFC3986

原因是传递的参数里有一些特殊字符,比如{ }等,而 RFC 3986规范定义了Url中只允许包含英文字母(a-zA-Z)、数字(0-9)、-_.~4个特殊字符以及所有保留字符(RFC3986中指定了以下字符为保留字符:! * ’ ( ) ; : @ & = + $ , / ? # [ ])。

处理方式:

1、修改tomcat配置,把特殊字符给放行

  /conf/catalina.properties中,找到最后注释掉的一行 #tomcat.util.http.parser.HttpParser.requestTargetAllow=|  ,改成tomcat.util.http.parser.HttpParser.requestTargetAllow=|{},表示把{}放行

2、如果调用第三方的,tomcat不受自己管控,更好的一种方式还是把特殊字符转码

  比如 {} 转码之后就是 %7B%7D 这样就没问题了!

原文地址:https://www.cnblogs.com/whaleX/p/13995124.html