如果 Mulesoft 4 中的 POST 请求中存在 id 字段,如何抛出错误消息

How to throw an error message if id field exist in POST request in Mulesoft 4

如何检查 post 请求中的 ID 是否不应包含在内。 我有 2 个场景。

请求正文:

 {
    "name"="John"
 }

预期结果:

"Success"

请求正文:

 {
    "id"="323",
    "name"="Jane"
 }

预期结果: "id field should not be specified."

以下代码将帮助您找到所需的东西

%dw 1.0
%output application/json
---
{
    output: 'ID field should not be specified' 
    when payload.id? otherwise 'Success'
}

在 mule 4 中:when otherwise 已被 if else 替换。 这是数据编织代码

%dw 2.0
output application/json
---

if (payload.id?)
  { result: "ID field should not be specified" }
else { result: "Success" }