Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring, redirect to external url using POST

In the following Spring 3.1 action, I've to do some stuff and add attribute to a POST request, and then redirect it to external URL through POST (I can't use GET).

@RequestMapping(value = "/selectCUAA", method = RequestMethod.POST)
public ModelAndView selectCUAA(@RequestParam(value="userID", required=true) String cuaa, ModelMap model) {
    //query & other...
    model.addAttribute(PARAM_NAME_USER, cuaa);
    model.addAttribute(... , ...);
    return new ModelAndView("redirect:http://www.externalURL.com/", model);
}

But with this code the GET method is used (the attributes are appended to http://www.externalURL.com/). How can I use the POST method? It's mandatory from the external URL.

like image 866
Accollativo Avatar asked Apr 01 '16 10:04

Accollativo


People also ask

How do I redirect a spring boot to another URL?

There are two ways you can do this. Two using RedirectView object. The return type of the method should be ResponseEntity<Void>. STEP2: Build response entity object with 302 http status code and the external URL.

How we can redirect to another page or controller in Spring MVC?

We can use a name such as a redirect: http://localhost:8080/spring-redirect-and-forward/redirectedUrl if we need to redirect to an absolute URL.

How do I redirect a URL to another URL in Java?

The sendRedirect() method of HttpServletResponse interface can be used to redirect response to another resource, it may be servlet, jsp or html file. It accepts relative as well as absolute URL. It works at client side because it uses the url bar of the browser to make another request.

How do I get a redirected URL from RestTemplate?

getValue(); System. out. println("redirectURL: " + redirectURL); return true; } return false; } }) . build(); HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient); RestTemplate restTemplate = new RestTemplate(factory); .......


1 Answers

Like @stepanian said, you can't redirect with POST. But there are few workarounds:

  1. Do a simple HttpUrlConnection and use POST. After output the response stream. It works, but I had some problem with CSS.
  2. Do stuff in your controller and after redirect the result data to a fake page. This page will do automatically the POST through javascript with no user interaction (more details):

html:

<form name="myRedirectForm" action="https://processthis.com/process" method="post">
    <input name="name" type="hidden" value="xyz" />
    <input name="phone" type="hidden" value="9898989898" />
    <noscript>
        <input type="submit" value="Click here to continue" />
    </noscript>
</form>
    <script type="text/javascript">

        $(document).ready(function() {
            document.myRedirectForm.submit();
        });

    </script>
like image 81
Accollativo Avatar answered Oct 04 '22 04:10

Accollativo