1

I want to implement this example with Spring:

@PostMapping(value = "/redirect/to_payment/{token}")
public ModelAndView handleRedirectMessage(@PathVariable("token") String token,
        @RequestBody Transaction transaction, HttpServletRequest request) throws Exception {

    String url = "http://www.someserver.com";
    String post_token = "1234561234543322";

    // Open here the link and redirect the

    return new ModelAndView("redirect:" + url); 
}

How I can open this link, sent the post_token as POST param and return the opened page to the user?

Is there some way to implement this solution for the user? As the second solution can I return this page to the user and include the post_token as a param?

djm.im
  • 3,295
  • 4
  • 30
  • 45
Peter Penzov
  • 1,126
  • 134
  • 430
  • 808

3 Answers3

1

You can use okhttp3 dependency and send an http request from server and then return the response body of okhttp object to the client.

Here is an example:

@PostMapping(value = "/redirect/to_payment/{token}")
  public ModelAndView handleRedirectMessage(@PathVariable("token") String token,
          @RequestBody Transaction transaction, HttpServletRequest request) throws Exception {

    String url = "http://www.someserver.com";
    String post_token = "1234561234543322";

    OkHttpClient client = new OkHttpClient();

    Request request = new Request.Builder()
         .url(url)
         .post(null) // because u have no body
         .addHeader("Authorization", post_token)
         .addHeader("cache-control", "no-cache")
         .build();
    Response response = client.newCall(request).execute();

    return new ModelAndView(response.body().toString()); // or something like this
}

Of course you have to handle IOException and the body method at the end may be a little different.

One tip: you can use postman to generate OkHttp or Unirest request code for you by simulating your request easily.

AntoineB
  • 4,535
  • 5
  • 28
  • 61
Seyed Ali Roshan
  • 1,476
  • 1
  • 18
  • 37
0

You would have to actually do the post request to external server (eg. Using Apache HttpClient or for simple cases JSoup) and return response body to original caller.

If you want to perform a kind of eg login on behalf of user, you would also have to perform controlled session hijack.

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
0

To ask to redirect a user's browser, you need to send JS code and on page load event, you have to call redirect code like this

function redirectPost(url, data) {
    var form = document.createElement('form');
    document.body.appendChild(form);
    form.method = 'post';
    form.action = url;
    for (var name in data) {
        var input = document.createElement('input');
        input.type = 'hidden';
        input.name = name;
        input.value = data[name];
        form.appendChild(input);
    }
    form.submit();
}
// call on window load
 redirectPost('http://www.someserver.com', { post_token: '1234561234543322' });
divyang4481
  • 1,584
  • 16
  • 32