Karun Madhok
Karun Madhok

Reputation: 91

How to send Token with POST request to Django REST api from Android App?

I am using Django REST framework TokenAuthentication. I am able to recieve token from api when i sends username and password. But how to send that token with every request from android app to the server?

Upvotes: 1

Views: 1245

Answers (2)

Jeroen Brock
Jeroen Brock

Reputation: 191

You can save the Token in your shared preferences so you can access it everywhere in your app: so you dont have to give it with the intent everytime

final SharedPreferences prefs = this.getSharedPreferences( "PACKAGE NAME", Context.MODE_PRIVATE);

you can put the Token in it by

prefs.edit().putString("Token",tokenvalue).apply();

and retrieve it by

prefs.getString("Token","DEFAULT VALUE");

You can send the Token with your request by putting it in the Headers:

HttpPost httpPost = new HttpPost(); httpPost.setHeader("authorization",prefs.getString("Token","DEFAULT VALUE");

Upvotes: 2

Mark Galloway
Mark Galloway

Reputation: 4151

You need to include the token in the header you send with the request, where the random string is your token:

Authorization: Token 1af538baa9045a84c0e889f672baf83ff24

Upvotes: 0

Related Questions