Showing posts with label rest. Show all posts
Showing posts with label rest. Show all posts

8.23.2017

Use httpie to play with your APIs

Got tired of using curl. I switched to httpie.

1. Install
$ pip3 install httpie

2. I use jwt
$ pip3 install -U httpie-jwt-auth

3. First get token
$ http POST :8001/api-token-auth/ 'username=admin' 'password=abc123'

4. Now set env variables
$ export JWT_AUTH_TOKEN=your_token
$ export JWT_AUTH_PREFIX=JWT

5. Now do whatever
$ http --auth-type=jwt :8001/users/

6. If this error is encountered...
SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed

Use --verify=no as in...

$ http --verify=no POST ...

10.26.2016

DRF JWT Authentication

i.e., Django REST Framework with JSON Web Token Authentication. Got the solution from here: http://getblimp.github.io/django-rest-framework-jwt/ http://zqpythonic.qiniucdn.com/data/20141006233346/index.html
$ pip install djangorestframework-jwt

In settings.py:
REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
    ),
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
    ),
}

In urls.py:
from rest_framework_jwt.views import obtain_jwt_token, refresh_jwt_token

urlpatterns = patterns(
    # ...
    url(r'^api-token-auth/', obtain_jwt_token),
    url(r'^api-token-refresh/', refresh_jwt_token)
)

To test:
$ curl -X POST -d "username=admin&password=password123" http://localhost:8000/api-token-auth/
$ curl -X POST -H "Content-Type: application/json" -d '{"username":"admin","password":"password123"}' http://localhost:8000/api-token-auth/
$ curl -H "Authorization: JWT " http://localhost:8000/protected-url/