Apache camel 如何以 "body" 格式提交正文
Apache camel how to submit body in "body" format
使用 Apache camel,我有 Rest 组件。看起来像:
<post uri="/body" method="POST">
<description>Here is post method</description>
<param name="save" type="body" dataType="string"/>
<route>
<process ref="postRedirectProcessor" />
<to uri="direct:commonRoute" />
</route>
</post>
并且此端点处理这样的精细请求:
curl -i --data "b=hereisbody" http://localhost:8080/body (works fine, but I don't need it)
(我可以看到它进入 postRedirectProcessor,这很好)。但这不是我想要的。我希望它处理这样的请求:
curl -i --data "hereisbody" http://localhost:8080/body (doesn't work, causes 405)
我的意思是,"data" 的格式是 而不是 ,就像 k=v&k2=v2,但它只是字符串,就像在示例中一样(例如 --data "something")。
它会导致异常,不会进入 postRedirectProcessor。
2020-04-10 18:43:09,716 ERROR [http-nio-8080-exec-6] - ,,, - Servlet.service() for servlet [CamelServlet] in context with path [] threw exception
java.lang.IllegalArgumentException: Invalid parameter, expected to be a pair but was hereisbody
at org.apache.camel.http.common.DefaultHttpBinding.readFormUrlEncodedBody(DefaultHttpBinding.java:272) ~[camel-http-common-2.24.3.jar:2.24.3]
at org.apache.camel.http.common.DefaultHttpBinding.readRequest(DefaultHttpBinding.java:116) ~[camel-http-common-2.24.3.jar:2.24.3]
at org.apache.camel.http.common.HttpMessage.<init>(HttpMessage.java:56) ~[camel-http-common-2.24.3.jar:2.24.3]
at org.apache.camel.http.common.CamelServlet.doService(CamelServlet.java:187) ~[camel-http-common-2.24.3.jar:2.24.3]
at org.apache.camel.http.common.CamelServlet.service(CamelServlet.java:79) ~[camel-http-common-2.24.3.jar:2.24.3]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
我想,param type="body" 就像 xml 发布的那样,可以解决问题,但运气不好。
Curl 默认在 --data
和 Content-Type: application/x-www-form-urlencoded
header 中发送数据。参见 How to post raw body data with curl?
x-www-form-urlencoded
必须采用 key/value 格式 (specification)。这就是抛出异常的原因。
The name is separated from the value by =
and name/value pairs are separated from each other by &
.
要发送原始数据,您需要指定另一个 Content-Type
。
curl -v -i -H "Content-Type: text/plain" --data "hereisbody" http://localhost:8080/body
使用 Apache camel,我有 Rest 组件。看起来像:
<post uri="/body" method="POST">
<description>Here is post method</description>
<param name="save" type="body" dataType="string"/>
<route>
<process ref="postRedirectProcessor" />
<to uri="direct:commonRoute" />
</route>
</post>
并且此端点处理这样的精细请求:
curl -i --data "b=hereisbody" http://localhost:8080/body (works fine, but I don't need it)
(我可以看到它进入 postRedirectProcessor,这很好)。但这不是我想要的。我希望它处理这样的请求:
curl -i --data "hereisbody" http://localhost:8080/body (doesn't work, causes 405)
我的意思是,"data" 的格式是 而不是 ,就像 k=v&k2=v2,但它只是字符串,就像在示例中一样(例如 --data "something")。
它会导致异常,不会进入 postRedirectProcessor。
2020-04-10 18:43:09,716 ERROR [http-nio-8080-exec-6] - ,,, - Servlet.service() for servlet [CamelServlet] in context with path [] threw exception
java.lang.IllegalArgumentException: Invalid parameter, expected to be a pair but was hereisbody
at org.apache.camel.http.common.DefaultHttpBinding.readFormUrlEncodedBody(DefaultHttpBinding.java:272) ~[camel-http-common-2.24.3.jar:2.24.3]
at org.apache.camel.http.common.DefaultHttpBinding.readRequest(DefaultHttpBinding.java:116) ~[camel-http-common-2.24.3.jar:2.24.3]
at org.apache.camel.http.common.HttpMessage.<init>(HttpMessage.java:56) ~[camel-http-common-2.24.3.jar:2.24.3]
at org.apache.camel.http.common.CamelServlet.doService(CamelServlet.java:187) ~[camel-http-common-2.24.3.jar:2.24.3]
at org.apache.camel.http.common.CamelServlet.service(CamelServlet.java:79) ~[camel-http-common-2.24.3.jar:2.24.3]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
我想,param type="body" 就像 xml 发布的那样,可以解决问题,但运气不好。
Curl 默认在 --data
和 Content-Type: application/x-www-form-urlencoded
header 中发送数据。参见 How to post raw body data with curl?
x-www-form-urlencoded
必须采用 key/value 格式 (specification)。这就是抛出异常的原因。
The name is separated from the value by
=
and name/value pairs are separated from each other by&
.
要发送原始数据,您需要指定另一个 Content-Type
。
curl -v -i -H "Content-Type: text/plain" --data "hereisbody" http://localhost:8080/body