Webhook Redirection and Delayed Responses

I’ve been playing with webhooks and realized that in addition to the on received and last node responses, it would be convenient to provide a redirect function. This would be particularly helpful when receiving form submissions as the hook could then redirect the user to a confirmation page.

Currently I’m working with static sites and using n8n for contact form submissions. I need to implement some javascript to perform an ajax form submission and redirection. (with or without processing the return value) It would be much more streamlined to simply post forms to a n8n webhook and have the client redirected by n8n.

Additionally, while significantly more complex, delayed responses would be interesting.

  1. Receive webhook and process data
  2. Run through an IF block
  3. Send webhook response based on IF conditions, or even a secondary node’s actions. (eg SMTP fails)
  4. Complete the webhook response, returning (or redirecting) based on above logic.

Yes, can see how that could be helpful! At least the first part. Can you please explain the second one with the “delayed responses”. I do sadly not totally understand that especially because it seems to sends two responses. Once in 3 and once in 4.

Sorry, you’re correct. To clarify:

  1. Receive an incoming webhook, collect any (post/etc) data, leave the connection open if possible (think long polling)
  2. Move to other nodes such as IF, an API call, etc
  3. Webhook Response node: Handle and return/close the connection from number 1 based on info passed by anything in number 2.

As long as there aren’t timeout issues I think this would work. Receive the request, leave connection open until any future node resolves it.

Ah in that case you are describing what can be done with a Webhook Trigger-Node in Response Mode: “Last Node”.

Here an example:

Oh, well that makes sense now. I saw that and thought “Last Node” meant previous. Come to think about it that doesn’t make sense for a trigger…

So if that works as I expect it does, that resolves the second part of my request.

Is redirection something that can be (easily) done?

A secondary feature would be to redirect based on the Last Node as well. Rather than returning, if you use a Last Node Redirect, the last node would simply return the URL to be used for the redirection.

Eg:
Webhook: Last Node Redirect (takes web site form submission)
Node: Send Email
If Successful: Return confirmation url for Last Node Redirect
Else: Return error url for Last Node Redirect

Ah great that at least one problem you have is solved!

About the redirect. I have to think a little bit about how I can implement that in a “good way”. Will update here if I came up with something. Sadly do not know how fast that will be.

Up-voted this for the redirection part.

I maintain a few static site generators and would love to offer my users an easy to install and configure form processor.

I was hoping something like the HTTP Request node could request the “thank you” page from a site and return that vs JSON response.

I prefer not to use any JavaScript on my own sites, if possible, so validating and serving response from n8n would be ideal. Granted, it’s probably not “proper” to not handle response in case n8n is down or can’t process the request, but users can opt to add extra JS in case wanting to cover that case.

Any news or solutions for the redirect?

Only if you want to redirect to a fixed page. Then you can set “Response Code” 303 and add a "Response Header" with the key Location` and value set to that fixed target URL.

Like in this example:

3 Likes

Thank you, Jan! That’s ok for me so far.

Great to hear! Have fun!

Hello @jan is there any way to dynamically put “Location” header on redirect?

Thank you,
Vitalie.

1 Like

Hi @Vitalie_Svet, maybe a little late, but yes you can do that.

Copy the following to node:

:v:

René

1 Like

That’s great :-1: thank you @renetheastronaut

When I use this redirection, even if I specify another website in the url, the response keeps redirecting me to my n8n url

Can you share your workflow/settings?