Redirect rule doesn't preserve request body

I set this Forwarding URL rule:

https://olddomain.com/connect/token*
To
https://newdomain.com/connect/token/$1

So every request to the old domain will get forward to the new domain.
For GET request it works fine, but for POST requests the request body doesn’t pass and the request arrives empty to it’s destination.

How can I include the request body?
Thank you

That’s just how HTTP redirects work. A forwarding rule/302 or 301 redirect is just responding to the request with 301 or 302 and with the location: header telling the client where to send their request instead. The server doesn’t actually do anything past that.

If the client isn’t programmed to send the same POST request body to the new URL when they receive a redirect, then no redirect configuration can help ‘include the request body’ for the client.

Now, this is likely possible with Workers - you could have a worker set up at the old URL and simply fetch() the new URL, however, that will NOT redirect the client since the server can’t respond to a post request and redirect at the same time.

1 Like

I use IIS and from my understanding a redirect won’t help because it’s good only for GET requests.

I use IIS as a reverse proxy in my server and there is an option for Rewrite requests.
I see that Cloudflare has an option for URL Rewrite, is this the same as IIS rewrite, would it preserve the request body?

I need to support an old API POST end point such as https://olddomain.com/connect that passes data in the request body.

Does a URL Rewrite Rule to https://newdomain/connect will preserve the request body?

Thank you

Sorry, but you can’t use a URL Rewrite Rule to change hostnames.

Is there any solution by Cloudlfare for this scenario?

You could probably use a Worker. Something like:

if (request.method === 'POST' && pathname === '/connect') {
  fetch('https://newdomain.com/connect', request);
}
1 Like

This is pretty much a repeat thread by the poster. Judge already recommended the fetch approach on the old thread, then Offir started a new thread. I’m going to merge them.

3 Likes

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.