Hi,
I have a requirement where i have to set different HTTP status codes based on each scenario like 200,201,204 etc., But when I am using this service pub.flow:setResponseCode, its setting the code but not displaying the response data. I have used pub.flow:setResponse2 along with this to set my response data, but its not getting displayed.
When i tried to remove pub.flow:setResponseCode, then my response data is shown but with HTTP status as 200:
You might consider using $httpResponse instead. From the docs:
You can set an HTTP response by adding a document reference to the pub.flow:HTTPResponsedocument type in the pipeline of the service and naming the variable as $httpResponse. Using the values set in the $httpResponse, Integration Server sends the response back to the client.
Provides a convenient way to set everything. The docs describe how to use it.
Specifically for 204, that code EXPLICITLY indicates that there is no response data.
HTTP Status 204 (No Content) indicates that the server has successfully fulfilled the request and that there is no content to send in the response payload body.
If you’re using 204, don’t send any response body. If you want to send a response body, use some other 2xx code (but not 205 as that also is a ‘no content’ response code).
Conventionally, you might consider an approach that is common for not found:
If a specific record is requested, return 404 not found.
If the request usually results in a list of records, but the request/query results in no records, return 200 and an empty array.