gpt4 book ai didi

java - Apache Camel如何以 "body"格式提交正文

转载 作者:行者123 更新时间:2023-12-02 08:44:59 27 4
gpt4 key购买 nike

使用 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)

我的意思是,“数据”的格式不像k=v&k2=v2,但它只是字符串,就像示例中的那样(例如- -数据“某事”)。

它会导致异常,它不会进入 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 必须采用键/值格式 ( 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

关于java - Apache Camel如何以 "body"格式提交正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61139668/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com