Prateek Shrivastava
Prateek Shrivastava

Reputation: 480

How we can modify headers in request Object using Filter

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.

Upvotes: 2

Views: 17218

Answers (2)

Josh
Josh

Reputation: 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);

Upvotes: -1

Rutesh Makhijani
Rutesh Makhijani

Reputation: 17235

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

Upvotes: 5

Related Questions