Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to read header from response and store it for every future request? #37

Closed
yduman opened this issue May 25, 2017 · 3 comments
Closed

Comments

@yduman
Copy link

yduman commented May 25, 2017

Hi,
I have a JWT token that I get from a login response. For authentication, I need to set that token for every request later on until that token gets deleted on logout.

How can I read that token, without providing it on JSON body and store it for every future request?

I am quite new with Android development and until now I had really good experience with volley. Now I am struggling at this use case. Any help would be appreciated!

Have a nice day!

@xesam
Copy link

xesam commented May 25, 2017

i think...

you can read the token header from NetworkResponse#headers :

@Override
    protected Response<String> parseNetworkResponse(NetworkResponse response) {
        String token = response.headers.get("token");
        ...
    }

if you want to set this token for every request ,you can define a new subclass of Request, just like StringRequest,and put the token in headers:

class TokenRequest extends Request{
    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        Map<String, String> headers = Collections.emptyMap();
        headers.put("xxxxxxxxxx", getToken());
        return headers;
    }
}

@yduman
Copy link
Author

yduman commented May 25, 2017

@xesam alright thank you for your help! I'll try this out, sounds good

@yduman yduman closed this as completed May 25, 2017
@saketdalvi
Copy link

saketdalvi commented May 14, 2020

Hi,
Can you please help with getting the JWT token, by sending my credentials with Volley?
Have a nice day!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants