2

I have a Spring Boot API (Stateless) with a Controller that receives a POST request, extracts the parameters of the POST request to send them through GET to my angular client. My question is if it's possible to send hidden parameters in HttpServletResponse.sendRedirect()?

What I have so far is this, but I do not want to show the parameters in the browser ...

@RequestMapping(value = "/return", method = RequestMethod.POST, headers = "Accept=text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
    @ResponseBody
    @Transactional
    public void returnData(UriComponentsBuilder uriComponentsBuilder, final HttpServletRequest request,
            final HttpServletResponse response) throws IOException {

        String parameter=request.getParameter("billCode");

        response.sendRedirect("http://localhost:4200/payment?parameterOne="+parameter);
    }

Update:

I can't uses HttpSession session = request.getSession(false); and then session.setAttribute("helloWorld", "Hello world") because session is Null

Many Thanks!

AlejoDev
  • 4,345
  • 9
  • 36
  • 67
  • Possible duplicate of [Pass Hidden parameters using response.sendRedirect()](https://stackoverflow.com/questions/17001185/pass-hidden-parameters-using-response-sendredirect) – Alien Nov 04 '18 at 18:33

1 Answers1

2

You can use the HTTP response header instead of sending the parameter in the queryString. An example:

@GetMapping(value="/")
public void init(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String billCode = request.getParameter("billCode");
    response.addHeader("parameterOne", billCode);
    response.sendRedirect("http://localhost:4200/payment");
}

To get the value from request:

String billCode = request.getHeader("parameterOne");

Or if you get from ajax with jQuery:

$.ajax({
    url:'/api/v1/payment'
}).done(function (data, textStatus, xhr) { 
    console.log(xhr.getResponseHeader('parameterOne')); 
});

Hope this helps.