This topic was automatically generated from Slack. You can find the original thread here.
I have an if-then conditional, but I want the different response bodies returned, and I can’t seem to get that:
This topic was automatically generated from Slack. You can find the original thread here.
I have an if-then conditional, but I want the different response bodies returned, and I can’t seem to get that:
how do I get a proper return body when using if-then.
To return different response bodies based on if-then (if/else) logic in a Pipedream workflow, you should use a code step (Node.js or Python) and write your conditional logic there. The value you return from the code step becomes the step’s output (the “return body”). Here’s how to do it in a Node.js code step: export default defineComponent({ props: { input: { type: "string", label: "Input value", }, }, async run({ steps, $ }) { if (this.input === "foo") { // Return this object if the condition matches return { message: "Input was foo", code: 200 }; } else { // Return a different object otherwise return { message: "Input was not foo", code: 400 }; } }, }); How it works: - The if statement checks your condition. - Each return sends a different response body depending on the condition. - The returned object is available as the step’s output, and can be referenced in later steps. If you want to send a specific HTTP response from an HTTP trigger step: Use $.respond() in your code step: export default defineComponent({ async run({ steps, $ }) { if (steps.trigger.event.body.value === "foo") { await $.respond({ status: 200, body: { message: "Input was foo" }, }); } else { await $.respond({ status: 400, body: { message: "Input was not foo" }, }); } }, }); - $.respond() lets you control the HTTP response status and body directly. Summary: - Use if/else in your code step to control the returned body. - return sets the step output. - Use $.respond() if you want to control the HTTP response directly. If you need more help or have a specific use case, let me know!
This still doesn’t make sense. I am unable to set the response within the if/else step. The last step in the flow has an http-response but it is not used.
I assume the return_http_response action is using $.respond, but if you want the response to be exported to the if/else step, you would need to return it
So try using a custom code step instead