In my application I want to redirect current request to my other server based on some condition

Problem: I have to redirect my Post request to my other server with all the headers and params and options.

For redirecting I am using below code

conn |> redirect(external: redirect_url)  |> halt()

Which doesnt retain all the headers, params and information.

How can I simply redirect to external url with everything intact?

redirect/2 sends an 302 status code (:found) by default, which means “a resource is available elsewhere”. Semantically for an intial post request this means “what you tried to create already exists on that url”. Therefore browsers usually do a GET request to the url forwarded to.

If you want to actually say “do the same request to this other url instead” you’ll need to set the status to 307.

|> put_status(:temporary_redirect)
|> redirect(external: redirect_url)  
|> halt()