9

I want to send a post request in java. I have seen examples for the post request by using Http Client. But i want use sendRedirect method.

For ex, https://processthis.com/process?name=xyz&phone=9898989898

I want to use post request to send those parameters. So, those params will not be visible to any one and at the same time i need to redirect my url to that url as,

response.sendRedirect("https://processthis.com/process");
Vamsi Krishna
  • 140
  • 1
  • 1
  • 6

3 Answers3

8

According to RFC2616 with HTTP/1.1 you can send 307 response code, which will make user-agent to repeat it's POST request to provided host. In your case just do

response.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
response.setHeader("Location", url);

response is your HttpServletResponse object.

Hope this helps.

adiga
  • 34,372
  • 9
  • 61
  • 83
Alex
  • 89
  • 1
  • 3
4

When a browser receives an HTTP redirect code it will always perform a GET or HEAD to the given url by standard. This is why data must be sent by query strings. If you want to simulate a redirect by POST, you can send to your client a form with the information you want and, on the page load event, you automatically submit the form using Javascript (commonly used to comunicate between different servers, used by SAML protocol for example). Here's an example:

<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>

Side note: If you already have the information in your server why are you sending a redirect instead of doing the given action? Maybe you want to implement a POST/REDIRECT/GET pattern?

João Simões
  • 1,351
  • 1
  • 10
  • 20
  • No. Actually i'm interacting with another server. Once user submits a form, i process data and then i need to redirect the user to third party payment page. When user submit a form with all required hidden fields it is working fine(example i tried). But, After i process those params at my side i want to manually redirect to third party payment page from my site( like form submit). Means, parameters should not be visible but request has to go. Is there any way to achieve. It might be silly. But i'm fresher. Please let me know. – Vamsi Krishna Feb 04 '14 at 12:05
  • That is one of the reason the logic above was created. You'll be able to POST data between those servers without sending them over query string. – João Simões Feb 04 '14 at 12:08
  • The idea with this approach is that the information you are sending will only be visible to your server (in your Database, etc) to the user (you are sending him his how information by HTML) and to the server you are posting the info (the client browser will automatically POST the info, simulating a manual POST by your user). Could you achieve that with my example? – João Simões Feb 04 '14 at 12:32
  • 1
    here's an example from an open source project that does something similar building the html in java which has the javascript and returns that in the response. https://github.com/keycloak/keycloak/blob/b478472b3578b8980d7b5f1642e91e75d1e78d16/common/src/main/java/org/keycloak/common/util/HttpPostRedirect.java – Phillip Fleischer Jan 05 '19 at 19:10
  • @Phillip Fleischer : I am trying to achieve the same thing. my requirement is to redirect to third party with parameter via POST in java. how should I do this. even if I create html form in java then what? please help. – Manan Kapoor Jan 29 '19 at 09:35
  • delayed response but if you look at the link java will return the html content which includes onload javascript which performs the form post. I've seen this in a bunch of libraries but it's really a simple html builder with javascript that is supported in most modern browsers. – Phillip Fleischer Mar 07 '19 at 06:14
1

I do not think this is possible. Why do not you use RequestDispatcher. This will work for you. Just set the parameters in request

request.setAttribute("message", "Hello test");
RequestDispatcher dispatcher = servletContext().getRequestDispatcher(url);

OR - The HTTP spec states that all redirects must be in the form of a GET (or HEAD). You can consider encrypting your query string parameters if security is an issue.

OR - another option, set the parameters in your session in servlet, if you have session. Then get it from session after you redirect to the required page.

A Paul
  • 8,113
  • 3
  • 31
  • 61