java - 如果未提供内容类型,则在 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&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
交换属性以防止 Camel 将请求参数绑定到 Camel 标头。true
推荐阅读
- r - 使用 R 将文本文件转换为表格
- python - ASCII动画闪烁python
- powershell - 从 CSV 用户列表中获取所有日历子文件夹
- r - R中的Web抓取教育数据
- javascript - 我如何在 firebase 上更新我的 react 项目主机?
- c - 如何在 C 编程中的单个脚本中写入、读取和删除文件
- java - 我对回收站视图和碎片有疑问?
- r - 如何自动选择一个好的 ylim 来读取 R 中 ggplot2 中的 geom_labels
- python - 如何制作 selenium python 脚本以继续单击按钮?
- python - 安装 turbodbc 时遇到问题