How to redirect a http POST with urlrewritefilter

3.7k Views Asked by At

I have a question about the urlrewritefilter and until now I could not find anything about it in the net.

I want to redirect a http POST in Tomcat7. Here is an example...

The call is a HTTP POST to an ULR like

http://localhost:8080/oldApplication/Example?a=123&b=2

This call also contains some content either as xml or json. The filter is configured well as it works and the urlrewrite.xml contains:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN"
"http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd">

<urlrewrite use-query-string="true">
    <rule>
        <condition type="method">POST</condition>
        <from>^(.*)$</from>
        <to type="redirect">/newApplication$1</to>
    </rule>
</urlrewrite>

In the access log I can see that a call to

http://localhost:8080/oldApplication/Example?a=123&b=2

gets redirected to

http://localhost:8080/newApplication/Example?a=123&b=2

Fine until now. The problem is that the rewrite changes the method, so that the new url gets called with a HTTP GET instead of a HTTP POST. I tried to add a condition on the method but got still a HTTP GET after the rewrite.

Does anybody know how to configure the rewritefilter to avoid this?

1

There are 1 best solutions below

0
On

You are using the type attribute redirect on type="redirect"

This attribute is equivalent to HttpServletResponse.sendRedirect() that actually does a new request to the destination using the GET method, so all parameters are lost along with the HTTP method.

The default value for this attribute if not informed is forward that is equivalent to HttpServletRequest.getRequestDispatcher(url).forward()

Forwarding will keep all request parameters and also the HTTP method.

So, in order to obtain the desired result you have to omit your type attribute or set it to forward.

<to>/newApplication$1</to>

or

<to type="forward">/newApplication$1</to>