骆驼如何绕过请求头进入http dsl组件请求?

时间:2021-01-30 10:23:09

标签: http apache-camel query-string

我在使用骆驼 http 请求时遇到了以下问题。我想保留 url 查询字符串参数的值,而不将其传递给需要在路由上完成的另一个 http 请求。在向外部 api 发出 http 请求后需要该值来处理数据。以下是对问题的澄清:

rest("/api")//We get requests as /camel/api?param1=xyz...
    .get()
        .route()
        .setHeader(Exchange.HTTP_METHOD, constant("GET"))
        .setHeader("Accept-Encoding", constant("gzip"))
        .setHeader("Accept", constant("*/*"))
        .removeHeader(Exchange.HTTP_URI)//Remove this
        //How do I prevent the {header.param1} from being passed to the following http request but still be able to use it after the request on the route?
        .to("https://someapi.org/api/...")
        //To process the result, we need the original {header.param1} value from the request to this /camel/api endpoint

    .endRest();

实现这一目标的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

如果您收到的参数仅在当前路由中需要,并且不应传递到任何其他端点,您也可以将它们复制到 to Exchange propertiesdelete the headers .

与消息头相反,Camel Exchange 属性不会传播到路由,当消息到达当前路由的末端时,它们会与 Exchange 一起删除。

.setProperty("param1", header("param1")) // create property from header
.removeHeader("param1") // remove the header

这是一种非常明确和透明的方式来执行此操作,但您必须在任何需要的地方执行此操作。因此,它适用于您想明确表示的特殊情况

另一方面,HeaderFilterStrategy 可防止将特定标头(基于模式)发送到您配置它的端点。因此,它非常适合您希望应用于特定类型的所有端点(例如,所有 HTTP 端点)的一般标头规则