So, I’m going to receive an API call at:
POST http://olddomain.com/api/call
Post contents in Raw Body
I need to redirect that to
POST http:://newdomain.com/api/call
With the same Raw Body contents. Seems there’s no way to redirect a POST server-side, and the only way to do it is to generate an HTML form and submit that form via JS. So, given that, I’m not even sure it’s possible for our API clients that are going to expect JSON returned back to them to render and submit that HTML/JS in order to redirect the POST.
So now I’m thinking that I may need to just translate the POST raw body contents into query string variables – the volume of data in the call is probably low enough that we can get away with this – and then maybe update the API call handler to accept GET parameters in this scenario.
None of this seems at all clean. Please tell me there is a better way.
UPDATE: The implementation requires curl compatibility, because the API client uses it.
To redirect a regular page from PHP, you could just run something like this:
But I’m not sure whether most clients would automatically pass the same POST data (according to Jay above, this would not carry the POST data). Regardless, this is assuming that the client handles HTTP redirects natively, which you can’t rely on for security reasons if the client is running on someone else’s machine. For instance, PHP’s
open_basedirsetting will not allowcurlto follow HTTP redirects.For this reason, you can’t expect the client to do the redirect for you. You’re going to have to be a middle-man, and make the new call to newdomain.com internally, then pass the response back down to the client. This probably isn’t an ideal solution, but when you’re hosting an API you have to be careful with what you assume the clients can or will do.