问题描述
如何清除Mule或Dataweave中的payload?我不想将其设置为 null 或空字符串。我只希望它是空的。这可能吗?
解决方法
使用 201 响应代码,您应该返回一个正文 - 这是预期的。有关详细信息,请参阅此答案:Create request with POST,which response codes 200 or 201 and content
这就是为什么如果您返回 201,即使回复中没有正文,邮递员等工具仍然会向您显示空响应正文。例如,如果您将响应代码交换为 204,则它不会显示响应正文。
这是一个示例应用,向您展示如何设置响应代码。请注意,在我的转换中,我设置了一个名为 responseCode
的变量,在我的 HTTP 侦听器中,我将响应代码设置为 vars.responseCode default 200
;这意味着,如果我明确设置变量 responseCode
,它将使用它,如果我不设置它,它将使用 200。尝试更改您的响应代码以查看它的外观。请注意,即使您确实设置了响应正文但返回了 204 状态代码,邮递员也不会显示正文。更改转换以将有效负载设置为 null
,您将看到相同的行为 - 201 期待一个正文,因此它表明它有一个空正文,204 什么也不显示。
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="6694e527-1d09-4c26-bc6e-0173e3fc6d04" >
<http:listener-connection host="0.0.0.0" port="8081" />
</http:listener-config>
<flow name="response-code-testFlow" doc:id="eac8817d-8806-4b8f-98cd-5ef277ba71be" >
<http:listener doc:name="Listener" doc:id="3ceb639d-d5cf-4933-8510-7cb6ca1b29fb" config-ref="HTTP_Listener_config" path="/test">
<http:response statusCode="#[vars.responseCode default 200]" />
</http:listener>
<ee:transform doc:name="Transform Message" doc:id="92d9dda8-2bc5-4528-a052-74d4886de29a" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
{
"message": "Hello world"
}]]></ee:set-payload>
</ee:message>
<ee:variables >
<ee:set-variable variableName="responseCode" ><![CDATA[204]]></ee:set-variable>
</ee:variables>
</ee:transform>
</flow>
</mule>
,
您好,在 mule 4 中,您可以通过在流程末尾设置一个空的有效负载来清除有效负载,并为 HTTP 状态代码 201 设置 HTTP 状态。 此外,您可以默认设置 HTTP 状态错误代码。
为此,您只需要在 HTTP 侦听器中添加如下配置(您可以将其用于任何 API)
<http:listener config-ref="HTTP_Listener_config" path="/api/*">
**<http:response statusCode="#[vars.httpStatus default 200]">
<http:headers>#[vars.outboundHeaders default {}]</http:headers>
</http:response>**
**<http:error-response statusCode="#[vars.httpStatus default 500]">
<http:body>#[payload]</http:body>
<http:headers>#[vars.outboundHeaders default {}]</http:headers>
</http:error-response>**
</http:listener>
第二,在流的末尾添加一个转换消息,如下所示,它将最终响应作为一个空负载,带有 HTTP 状态代码 201
<ee:transform xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{}]]></ee:set-payload>
</ee:message>
<ee:variables >
<ee:set-variable variableName="httpStatus" ><![CDATA[%dw 2.0
output application/java
---
201]]></ee:set-variable>
</ee:variables>
</ee:transform>
谢谢