AlejoDev
AlejoDev

Reputation: 3252

It's possible to send hidden parameters in HttpServletResponse.sendRedirect()?

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!

Upvotes: 2

Views: 1585

Answers (1)

Claudivan Moreira
Claudivan Moreira

Reputation: 212

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.

Upvotes: 2

Related Questions