Vihung
Vihung

Reputation: 13397

How Can I Read Cookies Coming Back in the Response to a RESTEasy Client?

I am using a RESTEasy Proxy client, and instantiating it with the following code.

final ResteasyClient client = new ResteasyClientBuilder().build();
final ResteasyWebTarget target = client.target(pUrl);
mClient = target.proxy(FooClientProxy.class);

where FooClientProxy is my proxy interface.

FooClientProxy defines a login method, as follows

@POST
@Path("/foo/login/{username}/{password}")
public abstract void login(@PathParam("username") String pUsername, @PathParam("password") String pPassword);

On logging in, with mClient.login(pUsername, pPassword);, I would like to see what cookies I have got back in the response.

Is there a way I can interrogate the proxy client to read the cookies in the response?

Upvotes: 0

Views: 1827

Answers (2)

lefloh
lefloh

Reputation: 10961

Why is your login method void? If you would return a Response you could read the Cookie like this:

mClient = target.proxy(FooClientProxy.class);
Response response = mClient.login(user, pw);
NewCookie cookie = response.getCookies().get(cookieName);

Upvotes: 1

KIC
KIC

Reputation: 6121

I think you can inject the HttpSession request where you should be able to access the cookies:

@POST
public String someRestCall(Postdata postdata, @Context HttpServletRequest request) {
  System.out.println(request.getCookies());
}

Respectively for your case

@POST
@Path("/foo/login/{username}/{password}")
public abstract void login(@PathParam("username") String pUsername, @PathParam("password") String pPassword,  @Context HttpServletRequest request);

I am not 100% sure since I can not test it here, but I am pretty sure :-)

Upvotes: 1

Related Questions