Skip to content

Instantly share code, notes, and snippets.

@LondonAppDev
Created October 1, 2018 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LondonAppDev/cbb9b1ea67e786bbf088d00ad2941127 to your computer and use it in GitHub Desktop.
Save LondonAppDev/cbb9b1ea67e786bbf088d00ad2941127 to your computer and use it in GitHub Desktop.
byob-profiles-api-requests-example
import requests
TOKEN_ENDPOINT = 'http://127.0.0.1:8000/api/login/'
PROFILE_ENDPOINT = 'http://127.0.0.1:8000/api/profile/'
def get_token(email, password):
"""Retrieve the token for logging in user"""
data = {'username': email, 'password': password}
res = requests.post(TOKEN_ENDPOINT, data=data)
return res.json()['token']
def get_profile(profile_id, token):
"""Retrive the profile for the logged in user"""
url = '{}{}/'.format(PROFILE_ENDPOINT, profile_id)
res = requests.get(url, headers={
'Content-Type': 'application/json',
'Authorization': 'Token {}'.format(token)
})
return res.json()
token = get_token('email', 'password')
profile = get_profile(1, token)
print(profile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment