Http headers with openfeign

50 views Asked by At

I need some help) I'm doing some little project with spring boot openfeign client. I have to do 4 http requests. From first GET request I need to get "set-cookie" header out of him get session id. This session ID I need to use in 3 subsequent request (POST, PUT and DELETE). Don't understand clearly how to do it with openfeign. I did it with rest template, but now I need help

enter image description here I did FeignClient interface (hopefully right) and now I have to implement these methods in service class

1

There are 1 answers

0
Ken.Zhang On

Try use apache HttpClient.

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-httpclient</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
</dependency>

Configure Feign to Use Apache HttpClient

Create a configuration class for your Feign client that customizes Apache HttpClient to capture cookies. This is just a simple example. In practice, your session should be stored in a better way.

@Configuration
public class FeignClientConfig {

    private static String sessionCookie = null;

    @Bean
    public Client feignClient(HttpClient httpClient) {
        return new ApacheHttpClient(httpClient);
    }

    @Bean
    public HttpClient httpClient() {
        return HttpClientBuilder.create()
                .addInterceptorLast((HttpResponseInterceptor) (response, context) -> {
                    // Capture 'Set-Cookie' header from the response
                    if (response.containsHeader("Set-Cookie")) {
                        Header setCookieHeader = response.getFirstHeader("Set-Cookie");
                        sessionCookie = setCookieHeader.getValue();
                    }
                })
                .build();
    }

    @Bean
    public RequestInterceptor requestInterceptor() {
        return template -> {
            // Apply the captured 'Set-Cookie' value to subsequent requests
            if (sessionCookie != null) {
                template.header("Cookie", sessionCookie);
            }
        };
    }
}

Define the Feign Client

Define a Feign client that will use the above configuration. This client will make the GET, POST, PUT, and DELETE requests.

@FeignClient(name = "apiClient", url = "http://example.com", configuration = FeignClientConfig.class)
public interface ApiClient {
    @GetMapping("/initial")
    String getInitialSession();

    @PostMapping("/subsequent")
    String makePostRequest();

    @PutMapping("/subsequent")
    String makePutRequest();

    @DeleteMapping("/subsequent")
    String makeDeleteRequest();
}

Use the Feign Client

Finally, use the configured Feign client in your service to perform the HTTP requests.

private final ApiClient apiClient;

    @Autowired
    public ApiService(ApiClient apiClient) {
        this.apiClient = apiClient;
    }

    public void executeRequests() {
        // The first request will capture the 'Set-Cookie' header
        String response = apiClient.getInitialSession();

        // Subsequent requests will include the captured cookie
        apiClient.makePostRequest();
        apiClient.makePutRequest();
        apiClient.makeDeleteRequest();
    }

FeignClientConfig: This configuration class customizes the Apache HttpClient used by Feign. It intercepts all HTTP responses to capture any Set-Cookie header and saves its value. Then, it uses a RequestInterceptor to apply this captured cookie to all subsequent requests made by the Feign client.

ApiClient: Defines the endpoints for your initial and subsequent requests.

ApiService: Uses the ApiClient to first make an initial request to capture the session cookie, then reuses this cookie for further requests.

The idea and steps are roughly the example above, and you need to deal with storing and invalidating cookies