Sambit
Sambit

Reputation: 8001

How to set add a new Header in Request in Spring Boot

Hi I am using Spring Boot 2.1.1 and I have written REST end points. If a request comes, it needs to be validated, based upon validation, I need add to add a new Request header as "NEW_SES_VAL_ID" and value as "12345". I have written interceptor also but I am unable to add to Request Header. Please help about how to add a new Request header in every incoming request in Interceptor in Spring Boot.

Upvotes: 2

Views: 19248

Answers (1)

dcalap
dcalap

Reputation: 1072

You can create an interceptor extending ClientHttpRequestInterceptor

Something like this:

public class MyCustomInterceptor implements ClientHttpRequestInterceptor {

    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
        // some code ...

        HttpHeaders headers = request.getHeaders();
        headers.add("MyHeader", "headerValue");

       // more code ...

       return execution.execute(request, body);
    }

   //Rest of the class
}

Hope it helps.

Upvotes: 1

Related Questions