Trying to utilize a webhook to serve a basic web page, but I'm failing due to unintentional redirect

I’m trying to utilize a directory structure name to pass to webhook nodejs code and do something with it.
Here’s my POC:
webhook.m.pipedream.net/


 props: {
    path: {type: "string"}
  },
  async run({ steps, $ }) {
    //crop leading slash
    let path = this.path.split("/")[1]
    console.log(path)

    if (path === "") {
        await $.respond({
            status: 200,
            body: "You've landed on the root of the domain",
        })
      }
    } else {
        await $.respond({
            status: 200,
            body: path,
        })
      }

My problem is that my webhook gets called twice on any path other than “/”. First time for that path, and immediately redirects to the root. So, if I enter:
webhook.m.pipedream.net/hello

the webhook gets called twice, first time for hello and then for “/”, and “You’ve landed on the root of the domain” is served. I never see the “hello” in my browser.

I’ve tried various permutations of the following code:

         await $.respond({
            status: 302,
            redirect: "manual", // doesn't seem to make any difference
            headers: {
              "Location": "/report" // this gets me into an infinite loop
            },
            body: body_of_report,
          })

Please advise,
Thank you,

Hi @outbakery,

It appears you’re encountering an issue with handling paths in your webhook and managing responses based on the path accessed. The problem you’re facing with the webhook being called twice, first with the specified path and then immediately redirecting to the root, might be related to the way the Pipedream platform or the client (browser) handles redirects or expects responses.

Given the code snippet you’ve shared, there seems to be a syntax error in your conditional blocks which might be causing unexpected behavior. Specifically, there’s an extra } after the first if block that should be removed. Additionally, the approach to handling paths and redirects might need adjustment.

Here’s a revised version of your code with the syntax error fixed and a simplified logic that directly responds based on the path without attempting a redirect. This should help in debugging the issue by eliminating the redirect complexity:

export default defineComponent({
  props: {
    path: { type: "string" }
  },
  async run({ steps, $ }) {
    // Crop leading slash
    let path = this.path.split("/")[1];
    console.log(path);

    if (path === "") {
      await $.respond({
        status: 200,
        body: "You've landed on the root of the domain",
      });
    } else {
      await $.respond({
        status: 200,
        body: path,
      });
    }
  }
});

This code assumes that this.path correctly captures the path part of the URL accessed. However, it’s important to note that handling paths directly in webhook code like this might not be fully supported depending on how Pipedream’s infrastructure parses and routes incoming HTTP requests to your webhook.

If you continue to face issues with the webhook being called twice or not behaving as expected, it might be beneficial to review Pipedream’s documentation on HTTP sources and webhooks to ensure you’re using the platform’s capabilities as intended. Additionally, considering reaching out to Pipedream’s support for more specific guidance related to their platform’s behavior.

For further assistance or to explore more support options, please visit Support - Pipedream.

I’m a bot powered by Pipedream and GPT-4. I’m still learning, so please double-check my answers and code! Learn how to build your own.