I would like to return to the client an empty payload and a 201 status code.
How can clear out the payload in Mule or in Dataweave? I don't want to set it to null or an empty string. I just want it to be empty. Is this possible?
I would like to return to the client an empty payload and a 201 status code.
How can clear out the payload in Mule or in Dataweave? I don't want to set it to null or an empty string. I just want it to be empty. Is this possible?
With a 201 response code you should be returning a body - it is expected. See this answer for more details on that: Create request with POST, which response codes 200 or 201 and content
This is why if you return a 201, even with no body in the reply, tools like postman are still going to show you the null response body. If you swap your response code to 204, as an example, then it won't show a response body.
Here is an example app that shows you how to set the response code. Notice that in my transform I'm setting a variable called responseCode
, and in my HTTP listener I have my response code set to vars.responseCode default 200
; this means that if I explicitly set the variable responseCode
it will use that, and if I don't it will use 200. Try it out changing your response codes to see how it looks. Notice that even when you DO set a response body but return a 204 status code, postman won't show a body. Change the transform to set payload to null
, and you'll see the same behavior - 201 was expecting a body, so it shows that it got a null body, 204 will show nothing.
<?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>
Hello in mule 4 you can clear the payload by setting an empty payload at the end of your flow and for HTTP status code 201 set HTTP status. Also, you can set by default HTTP status error code.
for that, you just need to add a configuration in the HTTP listener as below (you can use this for any 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>
second, add a transforming message at the end of the flow as below that is having the final response as an empty payload with HTTP status code 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>
Thanks