(...) (...) (...)

Spring POST form data sends  as escape character for special characters not present in the browser request

650 views Asked by At

I'm using a spring jsp form to send some data back to a controller.

<head>
    <meta charset="utf-8">
</head>
(...)
<spring:url value="pathA" var="formActionVar"/>
<form:form action="${formActionVar}" modelAttribute="modelA" id="formIdA" method="post">
    <input name="itemA" value="${modelA.itemA}" autocomplete="off"/>
    <input name="itemB" value="${modelA.itemB}" autocomplete="off"/>
    <button id="sendBtn" type="submit">
</form:form>

Looking at the browser request, the form data with Content-Type: application/x-www-form-urlencoded is as follows:

itemA=12%C2%A7&itemB=potatoes

The problem is that itemA is being converted to 12§ (like an escape char to all special chars, §, £, ...) meaning that there is something in between the browser request and the server doing that conversion.

Custom class that extends CharacterEncodingFilter:

public class CustomCharEncodingFilter extends CharacterEncodingFilter {

    public CustomCharEncodingFilter() {
        super("UTF-8", true, true);
    }
    
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        System.out.println(request.getCharacterEncoding()); // <-- null
        System.out.println("Encoded param: " + request.getParameter("itemA")); // <-- 12§

        request.setCharacterEncoding("UTF-8");              // <-- tested with these 2 lines but no effect
        setForceEncoding(true);
        
        System.out.println(request.getCharacterEncoding()); // <-- UTF-8

        System.out.println("Encoded param: " + request.getParameter("itemA")); // <-- 12§
        super.doFilterInternal(request, response, filterChain);
    }
}

But as you can see the value was already previously converted somewhere disregarding the character encoding. The weird part is that doing the request using ajax with a parsed form representation does not convert the 12§ value to 12§ and the controller gets the correct value.

Edit: Just a quick note to mention that using enctype="multipart/form-data" in the jsp form changes the itemA value to it's clear form (12§) but still gets the value to be "server-side rendered" as 12§

0

There are 0 answers