Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

fetching headers

Avatar

Level 4

Hi team,

 

How to fetch header "login-token" from response in java programmatically?

 

 

Thank you.

1 Accepted Solution

Avatar

Correct answer by
Level 5
5 Replies

Avatar

Community Advisor
Cookie[] cookies = request.getCookies();

if (cookies != null) {
 for (Cookie cookie : cookies) {
   if (cookie.getName().equals("login-token")) {
     //do something
     //value can be retrieved using #cookie.getValue()
    }
  }
}

 

 

request.getHeader("login-token");


Arun Patidar

Avatar

Level 4

Thanks for the prompt reply Arun,

I have done that but not able to find the cookie "login-token" from response.

 

I am trying to hit Assets api using java code with access token .. getting 401 unauthorised, But in postman I am able to fetch api because In the header field login-token cookie is there in postman.

 

even when hitting assets api with basic auth also getting 401 unauthorised.

Avatar

Employee Advisor

@sriram_1 hi,

 

If you are using java.net.URLConnection, you can use :

conn.getHeaderField("login-token");


If you are using Apache HTTPClient , you can use :

response.getFirstHeader("login-token").getValue();


Thanks,

Milind

Avatar

Level 4

Hi miland,

I have used the same but there is no header field named "login-token" in response.

 

 

Avatar

Correct answer by
Level 5

@sriram_1  - Check this for reference - 

https://gist.github.com/justinedelson/10878245

 

Thanks