1

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?

Dale
  • 1,289
  • 3
  • 16
  • 36
  • What do you mean by empty payload? Eg, in JSON, an empty payload might be considered `{ }`, an empty object. Or `[ ]`, an empty array. – Michael Jones Jan 19 '21 at 16:32
  • @MichaelJones I mean empty as in there should be nothing in the response body. Is that fairly standard for a POST to send back nothing? Or do I need to send back {} or [] or "" or something equally meaningless? – Dale Jan 19 '21 at 16:37
  • 1
    POST with a HTTP 201 response SHOULD ideally be sending enough information about the entity/resource created. It would mean that the request has been fulfilled and has resulted in one or more new resources being created. The primary resource created by the request is identified either in a Location header field or Request URI. In case you plan to send nothing/additional content in the response, from the action performed by the server; please do use HTTP 204. Any of {} or [] should equate to an empty payload. – Salim Khan Jan 19 '21 at 16:49

2 Answers2

4

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>

Michael Jones
  • 1,900
  • 5
  • 12
0

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

Anurag Sharma
  • 780
  • 7
  • 31