2

I need to create a Filter and modify header values set in request Object. How we can modify headers in request Object using Filter?, there is no setHeader method available in request Object.

Prateek Shrivastava
  • 450
  • 2
  • 5
  • 17

2 Answers2

5

You can use javax.servlet.http.HttpServletRequestWrapper to wrap the HttpServletRequest object passed by the server.

In the wrapper class you need to override getHeader method and return modified value of header.

You can refer to similar post over here Modify request parameter with servlet filter

Community
  • 1
  • 1
Rutesh Makhijani
  • 17,065
  • 2
  • 26
  • 22
-1

You can import Collectors using

import java.util.stream.Collectors;

Add the method below into your class.

private Map<String, String> convertHeadersToLowerCase(Map<String, String> headers) {
    return headers
            .entrySet()
            .stream()
            .collect(Collectors.toMap(entry -> entry.getKey().toLowerCase(), entry -> entry.getValue()));
}

Then before returning headers in the response, you should ensure they are converted before returning by adding the following into the Controller of your Response method:

requestHeaders = convertRequestHeadersToLowerCase(requestHeaders);

Shanteshwar Inde
  • 1,438
  • 4
  • 17
  • 27
Josh
  • 1
  • 2