获取 Apache Camel 中的 Http 正文,如果未显示内容类型,则无法正常工作



与阿帕奇骆驼一起工作,我发现了一些有趣的东西,我应该在我的情况下修复一些东西......所以,我有 POST 路由,它适用于不同类型的 url 等,我的意思是,我应该在我的处理器中获取 http 正文。

例如,我必须能够阅读以下内容:

curl -X POST -d'a=b'  http://localhost:8080/........  (DOESN'T WORK)

或(相同(

curl -X POST --data 'a=b'  http://localhost:8080/........ (DOESN'T WORK)

而且它不起作用!但是当我在请求中添加内容类型标头时,它可以工作:

curl -X POST -H"Content-type: application/json" -X POST -d'a=b'  http://localhost:8080/........  (WORKS)

甚至无效(!!!

curl -X POST -H"Content-type: xxxxxxx" -X POST -d'a=b'  http://localhost:8080/........ (WORKS)

有效

(以防万一,我如何在处理器中获取代码中的正文(:

String mainBody = exchange.getIn().getBody(String.class);
if(mainBody == null || mainBody.isEmpty()) {
LOG.error("EMPTY!");
} else {
LOG.error("FOUND " + mainBody);
}

奇怪的事情? 在调试期间意识到默认的内容类型是application/x-www-form-urlencoded。当我这样做时

curl -X POST -H"Content-type: application/x-www-form-urlencoded" -X POST -d'a=b'  http://localhost:8080/........ (DOESN'T WORK)

它也不起作用

所以我的问题是如何让它始终工作?我没有一些特殊的格式、内容类型等,我的东西应该只是将整个身体重定向到 3rd 方,它应该消耗所有内容类型(即使未指定(。我该怎么做?

附言我的 XML 配置是

<rests id="rests" xmlns="http://camel.apache.org/schema/spring">
<rest id="rest-custom">
.....
<post uri="/?matchOnUriPrefix=true&amp;bridgeEndpoint=true" method="POST">
<description>....</description>
<route>
<process ref="unknownPostRedirectProcessor" />
<to uri="direct:commonRoute" />
</route>
</post>
</rest>

这个"post"捕获所有post请求,除了描述的问题之外,它工作正常。

使用curl -v查看curlPOST方法的插入Content-Type: application/x-www-form-urlencoded

考虑将交换属性设置为trueExchange.SKIP_WWW_FORM_URLENCODED以防止 Camel 将请求参数绑定到 Camel 标头(如果您需要访问 HTTP 请求的原始正文(。

最新更新