I am trying to return a simple JSON/XML response after API binding to a workflow. I am getting a JSON response as follows.
{ "FirstMatch": null, "status": true, "statusMessage": "", "StepCount": 2, "progress": "[]", "enginePerf": "{\"setup\":46.8126,\"execute\":0.0,\"strike\":0.0}", "taskStartId": "ad1312ad-2492-4634-a66d-1123a478fd2192ac70", "apiPerf": "{\"execute\":78.1357}" }
Instead of this, Is it possible to return a custom JSON/XML message in a workflow?
From your message above, it seems that you have not connected your desired response to the variable bar FgResponseBody
property. The Variable bar comes with a bunch of defined properties for cases like this one.
You can achieve this by following the below steps:
Add another Variable Bar to your Workflow, close to the end of your workflow for readability.
Click on the + to add a property to the Variable bar and click on the dropdown within the box you would usually type the property name in. You will be presented with a list of defined Flowgear properties, which includes FgResponseBody
and many others for API inputs and outputs. (Screenshot for reference below)
FgResponseBody
from the API Outputs within the dropdown, you will also need to add FgResponseContentType
and FgResponseCode
to the new Variable bar.With the above in place, we can now set up our workflow to output a custom message from anywhere in our workflow. Connect your custom message output to the input of the new FgResponseBody
property and use either application/json
in the FgResponseContentType
property if you wish to return a JSON response, or use application/xml
if you wish to respond with XML. You can also return custom HTTP status using the FgResponseCode
defined property.
Below is a screenshot of a small example API workflow where I have set up a Formatter node for success and error. Both outputs are connected to FgResponseBody
, but in this case, only one will be pushed into the response, depending on the outcome of the IF node checking for success.
It is also worth noting that it is important to remember to catch all errors within the workflow and once caught, you can route them to the appropriate node to provide a meaningful response to your API consumer.
Here is the link to our article about APIs in Flowgear.
Please feel free to have a look through our help center as well. We have many articles and examples on all things Flowgear. You can also get directly to a node's help article by clicking on the dropdown arrow on any node, and then selecting Help for this Node.