0

I'm using the Quarkus Rest Client to communicate with an external service which uses two cookies to authenticate all requests. These two cookies are returned from an authentication API and from every subsequent API call. Is there a way to handle these cookies automatically? Currently I'm getting the cookies from the response object of the authentication API and I manually send them in every request using @CookieParam.

KyriacosP
  • 31
  • 1
  • 6

1 Answers1

0

I haven’t try it, but can’t you do something like this:

 //pseudo code !!!
@RestClient
public interface UsersClient {

  @POST
  String backendCall(@CookieParam("Token1") token1, @CookieParam("Token2") String token2)

  @POST
  Map<String,String> authenticate(String param)

   default String makeCall(String param) {
       var tokens = authenticate(param);
       return backendCall(tokens.get(0), tokens.get(1));
   }
}

From your service you inject this rest client and call the makeCall(...) method. That should authenticate you against your server, and use the tokens from the response and send these as cookies to the backend call.

Apologies for any mistakes in the code: I‘ve written it from my tablet. But I hope the idea is clear.

And also check the Microprofile Rest client documentation for more information:

https://download.eclipse.org/microprofile/microprofile-rest-client-2.0/microprofile-rest-client-spec-2.0.html#_sample_definitions

Serkan
  • 639
  • 5
  • 14
  • Thank you for your answer but this only covers one call after authenticate. it doesn't save the tokens for later calls. I was thinking of wrapping my rest client with an Singleton scoped a request scoped bean and hold the cookies there. – KyriacosP Dec 06 '21 at 08:32