11

I have a spring boot project which has some Rest APIs in it. I have two custom headers named request_date and tenant respectively.

I am trying to read the value of these headers in an interceptor, but it reads the value for only tenant and returns null for request_date.

Important

  • I use a filter to wrap the request object because I want to read the request body later.
  • There is a filter to add CORS headers.

When I run my project on localhost and debug the code, I am successfully able to read both the headers' values.

However, when I deploy my application in production and make the request using postman or some other client, the request_date header's value is always read as null.

I am not sure what seems to be the problem with this. I am using Spring boot v1.5.10.RELEASE and JDK 1.8

Note:

  • I have tried to rename the header to something like input_date. However, it still reads null.

The following is the relevant code

TestInterceptor

public class TestInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String requestDate = request.getHeader("request_date");
        String tenant = request.getHeader("Tenant");

        /*Perform some checks*/

        return super.preHandle(request, response, handler);
    }
}

CorsFilter

public class ApiCorsFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, PATCH, OPTIONS, DELETE, PUT");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers," +
                " X-Requested-With, Origin, X-Auth-Token, Tenant, request_date");
        response.addHeader("Access-Control-Expose-Headers", "X-Auth-Token, Content-Disposition");
        chain.doFilter(request, response);
    }
}

RequestCacheFilter

public class RequestCacheFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
                                    FilterChain filterChain) throws ServletException, IOException {

        HttpServletRequest req = new RequestWrapper(request);
        String body = ((RequestWrapper) req).getBody();

        /*Do some operation*/

        filterChain.doFilter(req, response);
    }
}
2
  • What if you call request.getHeaderNames()? Is it possible that null is set in the missing header? Commented Feb 28, 2019 at 10:40
  • I am not calling request.getHeaderNames(), yet I can read the value of the other headers. The problem is only specific to "request_date" header Commented Feb 28, 2019 at 10:43

2 Answers 2

8

The issue was with the nginx configuration.

I set the underscores_in_headers on; for the server and now it doesn't drop the headers with underscore in their names.

The solution suggested by @Karol Dowbecki works as well. When I renamed my header to requestDate, I was able to read the value successfully.

Sign up to request clarification or add additional context in comments.

Comments

7

Some network tools can drop headers that contain underscore in it's name. As per this answer underscore is a legal character but it's uncommon and sometimes tools require additional configuration to support it.

Rename your header to requestDate or request-date and see if it helps. If it works without underscore than inspect network route between client and server e.g. maybe there is a proxy that drops them?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.