Accessing all response headers

4,088 views
Skip to first unread message

Greg Roodt

unread,
Jun 9, 2013, 3:59:30 PM6/9/13
to volley...@googlegroups.com
I need access to some of the Cookies in an HTTP response to do some manual authentication Cookie handling. In a response, there are multiple Set-Cookie headers, but using Volley, it seems that they Headers are stored in a regular Map<String, String> and therefore some of the Set-Cookie headers are lost.

Is there a way to get access to all of the response headers? I think parseNetworkResponse is too late.
Is there a recommended way to work with Cookies? As I understand it, because of the dual HurlStack and HttpClient stack, manually keeping track of any Cookies inside the app is the best approach.

Thanks
Greg

Ficus Kirkpatrick

unread,
Jun 10, 2013, 12:17:56 PM6/10/13
to Greg Roodt, volley...@googlegroups.com
Volley doesn't support this right now. This was an intentional trade-off: less functionality for a simpler API.

Ficus




Greg

--
You received this message because you are subscribed to the Google Groups "Volley Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to volley-users...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Greg Roodt

unread,
Jun 10, 2013, 3:23:28 PM6/10/13
to volley...@googlegroups.com, Greg Roodt
I think the simple API is great, but I think that exposing the headers as a Map<String, String[]> or Multimap is going to be needed for a lot of applications. Especially since manual Cookie handling is necessary because of the dual Hurl and HttpClient stacks.

I can think of 3 headers that generally arrive with duplicate names:
Set-Cookie
Cache-Control
WWW-Authenticate

For any apps doing authentication, Set-Cookie and WWW-Authenticate are almost a must have.

Ficus Kirkpatrick

unread,
Jun 10, 2013, 4:20:34 PM6/10/13
to Greg Roodt, volley...@googlegroups.com
This is actually the first time I've heard about it in two years of Volley. So you may be right, but it just hasn't felt like the right move yet.

Ficus

Jonathan Steele

unread,
Jun 11, 2013, 3:11:24 PM6/11/13
to volley...@googlegroups.com
While I am looking to HurlStack and HttpClientStack, Request<?> has getHeaders().

You can subclass StringRequest to override getHeaders()

Greg Roodt

unread,
Jun 12, 2013, 2:11:28 AM6/12/13
to Jonathan Steele, volley...@googlegroups.com
Thanks, but that is for setting request headers.

Im looking for the response headers. They are available at the Network level, so I've had to override NetworkResponse and do a cast to get all of the response headers.




--
You received this message because you are subscribed to a topic in the Google Groups "Volley Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/volley-users/rNTlV-LORzY/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to volley-users...@googlegroups.com.

Greg Roodt

unread,
Aug 1, 2013, 5:08:03 AM8/1/13
to volley...@googlegroups.com
Just to follow up on this, overriding at the NetworkResponse level is too late.

Volley squashes the headers in HurlStack here:

for (Entry<String, List<String>> header : connection.getHeaderFields().
entrySet()) {
           if (header.getKey() != null) {
               Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
               response.addHeader(h);
           }
       }


So to solve my issue, I had to use a different HTTP library for the requests where I needed access to all response headers.

 
 

Olivier Dennemont

unread,
Aug 8, 2013, 5:17:59 AM8/8/13
to volley...@googlegroups.com
I am also seeing multiple Set-Cookie headers very frequently and need this for my application. That would be a great addition if Volley could support it.

@Greg, thank you for the code snippet, will try this as a temporary solution.

Audrey Troutt

unread,
Sep 19, 2013, 4:18:05 PM9/19/13
to volley...@googlegroups.com
I need to access a custom response header on one of the handful of kinds of requests in my app... this is a bummer. Otherwise I'm really enjoying using Volley.

Audrey Troutt

unread,
Sep 20, 2013, 10:49:36 AM9/20/13
to volley...@googlegroups.com
Duh--it's working fine for me. I just needed to make sure the (one custom) header was actually sent. :)

FYI, I wrote my own request object that extends Request<String> and I implemented my own parseNetworkResponse method. All I need back is the contents of this one header (the body says "success").

@Override

protected Response<String> parseNetworkResponse(NetworkResponse response) {

return Response.success(response.headers.get("X-MY-CUSTOM-HEADER"), null);

}

I'm not sure what to do about the cache entry so for now I'm making that null.

Jason Grife

unread,
Sep 30, 2013, 1:29:28 AM9/30/13
to volley...@googlegroups.com, Greg Roodt
I found this also to be a limitation of the functionality I need with Volley. Has there been any reconsideration for adding this feature in? Thanks

Ficus Kirkpatrick

unread,
Sep 30, 2013, 11:50:43 AM9/30/13
to Jason Grife, volley...@googlegroups.com, Greg Roodt
I'm planning on doing it over the holiday break. It's not a huge project from a code perspective, but I need to think through the process issues because it will be the first API-breaking change we do.

Duong Dang Chien

unread,
Oct 24, 2013, 3:44:48 AM10/24/13
to volley...@googlegroups.com, Jason Grife, Greg Roodt
Hi there,

Is there any news on this topic, I'm also using Volley and find out that it doesn't work well with cookies. If Volley exposes methods to capture cookies of responses and to inject those cookies in outgoing requests, it will be awesome.

Thanks.

Georgie Casey

unread,
Oct 29, 2013, 11:58:50 PM10/29/13
to volley...@googlegroups.com, Jason Grife, Greg Roodt
I edited Volley directly to get the response headers as an array instead of a Map. My code and an example activity is on GitHub at:

It's a hack but it works for me at the moment!

phil

unread,
Jan 5, 2014, 10:14:09 AM1/5/14
to volley...@googlegroups.com, Jason Grife, Greg Roodt
Any news an an official fix for this?

Christopher Lamont

unread,
Apr 5, 2014, 8:19:45 PM4/5/14
to volley...@googlegroups.com
As a follow-up to this question, does Volley support the global CookieHandler? I extended HurlStack like this and used it to create my RequestQueue, but it still seems to be ignoring Set-Cookie headers.

public class HurlCookieStack extends HurlStack {

    public HurlCookieStack(){
        super();
        CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
    }
}

On Sunday, June 9, 2013 3:59:30 PM UTC-4, Greg Roodt wrote:
On Sunday, June 9, 2013 3:59:30 PM UTC-4, Greg Roodt wrote:

Sean Briceland

unread,
May 20, 2014, 4:05:30 PM5/20/14
to volley...@googlegroups.com
Any updates on this discussion or alternative solutions aside from hacking Volley internals?

Samuel Lahti

unread,
May 20, 2014, 4:20:48 PM5/20/14
to volley...@googlegroups.com
I hacked something in to do something like this, you can check out my branch on 


in the toolbox folder, i added a folder called headers, there are 2 classes, ResponseHeaderRequest and ResponseWithHeaders.

I'm using gson to deserialize a json response straight to a POJO that I access in ResponseHeaderRequest#getData but you can swap out
how deserialization is done in the ResponseHeaderRequest#parseNetworkResponse

Gonzalo do Carmo Norte

unread,
Apr 21, 2017, 8:55:27 AM4/21/17
to Volley Users
Reply all
Reply to author
Forward
0 new messages