You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mule 3中用DataWeave 1遍历请求头并检查值的方法

在Mule 3中使用DataWeave 1遍历所有请求头并检查值

要实现遍历所有传入请求头、检查是否存在值包含特定内容的需求,你可以利用DataWeave 1的pluck和any函数处理message.inboundProperties(Mule 3中存储所有入站属性的对象,包含请求头),具体实现如下:

基础实现(包含系统属性)

如果不需要排除Mule自带的系统属性,直接检查所有入站属性的值:

%dw 1.0
%output application/java
---
message.inboundProperties pluck $ any (value) -> value contains "Moscow"

过滤系统属性的实现(推荐)

Mule的inboundProperties默认包含http.method、mule.*这类系统属性,建议先过滤掉这些,只检查自定义请求头:

%dw 1.0
%output application/java
---
message.inboundProperties 
  // 过滤Mule内置的系统属性
  filter ((value, key) -> !(key startsWith "http.") and !(key startsWith "mule."))
  // 提取所有属性值转为数组
  pluck $ 
  // 检查数组中是否有任意值包含目标字符串
  any (value) -> value contains "Moscow"

兼容非字符串类型值的实现

如果请求头可能存在数字等非字符串类型的值,需先转为字符串再检查,避免报错:

%dw 1.0
%output application/java
---
message.inboundProperties 
  filter ((value, key) -> !(key startsWith "http.") and !(key startsWith "mule."))
  pluck $ 
  any (value) -> (value as :string) contains "Moscow"

说明

  • 上述表达式均返回布尔值:true表示存在符合条件的请求头值,false表示不存在
  • 你可以将表达式结果存入变量,或直接在Choice组件的路由条件中使用

内容的提问来源于stack exchange,提问作者GettingStarted With123

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 16:40:55