繁体   English   中英

在 Apache 中获取 Http 主体,如果未提供内容类型,骆驼将无法正常工作

[英]Get Http body in Apache Camel not working if content-type not presented

使用 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 header 时它起作用了:

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>

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

使用curl -v查看curl插入Content-Type: application/x-www-form-urlencoded默认为POST方法。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM