首页 > 解决方案 > 如果未提供内容类型,则在 Apache Camel 中获取 Http 正文不起作用

问题描述

使用 apache camel,我发现了一些有趣的东西,我应该在我的情况下修复一些东西......所以,我有 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)

而且它不起作用!但是当我在请求中添加 Content-type 标头时它可以工作:

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);
}

奇怪的事情?在调试期间意识到默认的 Content-type 是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)

也不起作用

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

PS我的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>

此“帖子”捕获所有帖子请求,除了描述的问题外,它工作正常。

标签: javaapache-camelspring-camel

解决方案


用于curl -v查看默认curl插入Content-Type: application/x-www-form-urlencodedPOST方法。

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


推荐阅读