What is the Cause of Error 400 "$.respond() not called for this invocation" in My axios Request Code?

This topic was automatically generated from Slack. You can find the original thread here.

Hey ! Why am I getting the error Error - Request failed with status code 400 “$.respond() not called for this invocation” for this code?

import { axios } from@pipedream/platform”;

export default defineComponent({
async run({ steps, $ }) {
const webpageObject = steps.parse_webpage_info.$return_value.choices[0];
const isContentFalse = webpageObject.message.content === “FALSE”;
const url = isContentFalse ? “https://eoc7rfunjsze19k.m.pipedream.net” : “https://eo2pgqw3686t3sd.m.pipedream.net”;
const headers = {
“User-Agent”: “pipedream/1”,
“Content-Type”: “application/json”,
};
let data;

**if** (isContentFalse) {
  console.log("Routed to invalid content workflow");
  $.flow.exit("Invalid content submitted - ending workflow early");
} **else** {
  **const** recipe = webpageObject.message.content;
  data = {
    recipe_title: recipe.title,
    recipe_ingredients: recipe.ingredients,
    recipe_instructions: recipe.instructions,
    source_url: recipe.sourceUrl,
    source_name: recipe.source,
    author: recipe.author,
  };
  console.log("Routed to extract recipe workflow");
}

**const** response = **await** axios($, {
  method: "POST",
  url: url,
  headers: headers,
  data: data
});

**await** $.respond({
  status: 200,
  body: {
    msg: "Huzzah!",
  },
});

**return** response.data;

},
});

It seems like you’re getting a 400 error because the HTTP request made with axios is failing. The error message “Request failed with status code 400” is typically returned when there’s a problem with the request you’re sending to the server. In your case, it seems like you’re trying to send a POST request to either https://eoc7rfunjsze19k.m.pipedream.net or https://eo2pgqw3686t3sd.m.pipedream.net, depending on the condition isContentFalse. Here are a few things you could check: 1. Verify the URLs: Make sure that the URLs you’re sending the request to are correct and can accept POST requests. 2. Check the data you’re sending: Ensure that the data you’re sending in the request matches what the server expects. This includes the structure of the data and the data types. 3. Headers: Check if the server requires any specific headers that you might be missing. 4. Server logs: If you have access to the server, check the logs to see if they provide more information about why the request is failing. Also, the error “$.respond() not called for this invocation” is likely because you’re trying to use $.respond() in a code step. $.respond() is typically used in HTTP or webhook trigger steps to send a response back to the client. In a code step, you usually don’t need to use $.respond(). If you’re trying to return data from the code step, you can just use return. Please review these points and adjust your code accordingly. If you’re still having issues, please provide more details about the error and I’ll be happy to help further.

Check your sources on the HTTP trigger and HTTP responses. Recall that $.respond must be called in any branch of code when the HTTP trigger for the workflow is configured to expect a custom response. So review the code above to see if there are any branches where $.respond isn’t called.

You’re correct. In your code, there’s a branch where $.respond() isn’t called. When isContentFalse is true, you’re logging a message and then exiting the workflow using $.flow.exit(). However, you’re not sending a response back to the client using $.respond(). Here’s how you can modify your code to ensure $.respond() is called in every branch: import { axios } from "@pipedream/platform"; export default defineComponent({ async run({ steps, $ }) { const webpageObject = steps.parse_webpage_info.$return_value.choices[0]; const isContentFalse = webpageObject.message.content === "FALSE"; const url = isContentFalse ? "https://eoc7rfunjsze19k.m.pipedream.net" : "https://eo2pgqw3686t3sd.m.pipedream.net"; const headers = { "User-Agent": "pipedream/1", "Content-Type": "application/json", }; let data; if (isContentFalse) { console.log("Routed to invalid content workflow"); await $.respond({ status: 400, body: { msg: "Invalid content submitted - ending workflow early", }, }); $.flow.exit(); } else { const recipe = webpageObject.message.content; data = { recipe_title: recipe.title, recipe_ingredients: recipe.ingredients, recipe_instructions: recipe.instructions, source_url: recipe.sourceUrl, source_name: recipe.source, author: recipe.author, }; console.log("Routed to extract recipe workflow"); } const response = await axios($, { method: "POST", url: url, headers: headers, data: data }); await $.respond({ status: 200, body: { msg: "Huzzah!", }, }); return response.data; }, }); In this modified code, I’ve added a call to $.respond() before $.flow.exit() in the isContentFalse branch. This ensures that a response is sent back to the client in every branch of your code.

Ahhh…I think I see what might be happening. Thank you as always @UMT4G7E5P :pray:

You’re welcome! I’m glad I could help. If you have any more questions or run into any other issues, don’t hesitate to ask. Happy coding! :robot_face: