Spring redirecting to external URL using POST

In the next Spring 3.1 action, I have to do some things and add the attribute to the POST request and then redirect it to the external URL via POST (I cannot 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 uses the GET method (attributes are added to the http://www.externalURL.com/ ). How can I use the POST method? It is required for an external URL.

+5
source share
2 answers

As @stepanian said, you cannot redirect using POST. But there are a few workarounds:

  • Make a simple HttpUrlConnection and use POST. After the output of the response stream. It works, but I had a problem with CSS.
  • Make the material in your controller and redirect the result data to a fake page. This page will automatically do POST through javascript without user interaction ( more ):

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> 
+2
source

You cannot redirect using POST. You can send a POST request using Java code with a class, such as HttpURLConnection in action.

+3
source

Source: https://habr.com/ru/post/1246224/


All Articles