Q: Add POST data when doing a TEMPORARY_REDIRECT from another POST im developing a site that include a purchase screen. I will use a service similar to PAYPAL. I just need to send POST data to this service and the service will take care from that. 
The thing is, there are some fields I want to hide. So inputs type hidden are not an option. So I added a simple form with just one data, productId and then redirect the POST with the added data.
Now the problem, that data is being sent as URL parameters(I can see those values in the URL in the browser). I want those values (in the example, userId and reference) as POST data. How could I achieve this?
The code:
@RequestMapping(value = "/doPurchase", method = RequestMethod.POST)
public ModelAndView doPurchase(Principal principal, HttpServletRequest request,
        @RequestParam(value = "productId", required = true) String productId, RedirectAttributes attributes) {
    //
    Product product = productRepository.findByProductCode(productId);
    //
    // DO STUFF...
    //
    ModelAndView modelAndView = new ModelAndView("redirect:PAYMENT_SERVICE_URL");

    attributes.addAttribute("UserId", "USER_ID");
    attributes.addAttribute("Reference", "REF_ID");
    //
    // ETC ETC

    //
    request.setAttribute(View.RESPONSE_STATUS_ATTRIBUTE, HttpStatus.TEMPORARY_REDIRECT);
    return modelAndView;
}


A: It is impossible to redirect to do a POST request by 301/302.
307 can repeat the POST request, but you can not modify it.
Reference:

*

*https://softwareengineering.stackexchange.com/questions/99894/why-doesnt-http-have-post-redirect

*https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/307

*https://www.rfc-editor.org/rfc/rfc7231#section-6.4.7
Some bypass methods:

*

*Make your PAYMENT_SERVICE_URL accept both GET and POST requests.


*Make a intermediate page accept the redirected GET request, and generate a HTML POST form to PAYMENT_SERVICE_URL, and auto submit the form by JavaScript.


*If you are using SpringFramework, consider to use Flash Attributes: https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-flash-attributes


*If you really want to hide some parameters in a general way, you can store the values in a database table and pass the id of the row to your redirected page by GET request, and then load the parameters from the database table by id. Or, you can encrypt/decrypt the parameters.
