8

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.

Accollativo
  • 1,537
  • 4
  • 32
  • 56
  • 2
    Then it isn't a redirect. A redirect always results in a GET request. If you want a POST you will have to call the URL from the java code and pass the result along. – M. Deinum Apr 01 '16 at 10:13
  • See also http://programmers.stackexchange.com/questions/99894/why-doesnt-http-have-post-redirect – M. Deinum Apr 01 '16 at 10:27

2 Answers2

5

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>
Community
  • 1
  • 1
Accollativo
  • 1,537
  • 4
  • 32
  • 56
  • here's another example that backs this up, i liked this util class https://github.com/keycloak/keycloak/blob/b478472b3578b8980d7b5f1642e91e75d1e78d16/common/src/main/java/org/keycloak/common/util/HttpPostRedirect.java – Phillip Fleischer Mar 07 '19 at 06:17
3

You can't redirect with POST. You can send a POST request using Java code with a class like HttpURLConnection within the action.

stepanian
  • 11,373
  • 8
  • 43
  • 63
  • Ok, thanks, I used this example: http://stackoverflow.com/questions/3324717/sending-http-post-request-in-java my Spring controller *selectCUAA* what should return now? I need a redirect to the new web page. – Accollativo Apr 01 '16 at 14:14
  • You can redirect after the code-envoked POST is completed. – stepanian Apr 01 '16 at 16:46
  • Redirect with no attribute? In this way is it like calling a post and after a get, so the result will be only the last get? (the new url is a login page that need the parameter on post). However thanks, monday I will give a try – Accollativo Apr 02 '16 at 10:35
  • You can get the response from the post and return it to the user in the get response. – stepanian Apr 03 '16 at 08:11
  • I gave a look on the internet, but I become more confused. Could you show me an example modifying my *selectCUAA* controller? – Accollativo Apr 04 '16 at 10:23