4

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.

Josua Marcel C
  • 3,122
  • 6
  • 45
  • 87
kalenjordan
  • 2,446
  • 1
  • 24
  • 38

2 Answers2

3

To redirect a regular page from PHP, you could just run something like this:

header("Location: http://newdomain.com/api/call");
exit;

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_basedir setting will not allow curl to 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.

JMTyler
  • 1,604
  • 2
  • 21
  • 24
  • Good catch on the curl limitation. I'm wondering if it's possible to do some kind of domain-level redirection (like at the domain registrar level), that wouldn't run into that limitation. – kalenjordan Aug 09 '12 at 17:42
  • Nope, I think it's not possible. Domain forwarding does an HTTP Redirect actually, and the other option is masking, which I'm not even gonna consider:http://support.godaddy.com/help/article/422/forwarding-or-masking-your-domain-name – kalenjordan Aug 09 '12 at 17:44
2

There are two ways that I know you can do this. You can either (a) redirect the HTTP header from the .htaccess (a header redirect from PHP code will not carry the POST), or you can use some simple curl code to re-port to the new page in your PHP code.

Both these methods are summed up pretty nicely in this article: Redirecting an HTTP POST

Hope that helps!

Community
  • 1
  • 1
Jay El-Kaake
  • 618
  • 6
  • 11
  • Nice, htaccess redirect would have been a PERFECT solution if it wasn't for the curl limitation. I wonder if a dns-level redirect of some sort would do the trick. – kalenjordan Aug 09 '12 at 17:43