Skip to content

Instantly share code, notes, and snippets.

@foger
Created September 10, 2018 09:21
Show Gist options
  • Save foger/8a731461fa833de920b6672ce4d3921f to your computer and use it in GitHub Desktop.
Save foger/8a731461fa833de920b6672ce4d3921f to your computer and use it in GitHub Desktop.
CURL: token and request example

Get token:

curl -X POST -H "Content-Type: application/json" -d '{"username": "john", "password": "password"}' http://localhost:8080/api-token-auth/

Request:

curl -X GET -H "Authorization: Token <token>" http://localhost:8080/users/33456/ | json_pp

In Django urls.py add:

from rest_framework.authtoken.views import obtain_auth_token
urlpatterns = [
    path('api-token-auth/', obtain_auth_token),
]

settings.py:

INSTALLED_APPS = [
    'rest_framework',
    'rest_framework.authtoken',
    'rest_auth',
]

REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
    'PAGE_SIZE': 100,
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework.authentication.TokenAuthentication',
    ),
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAdminUser',
    ),
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment