Django REST API example implementation using Django REST Framework.
- Create a
.env
file containing Postgres credentials. Here are the default values:
POSTGRES_NAME=postgres
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
- Build and run the container for the project. Make sure it is working properly.
docker-compose up --build
- Close the container and do the migrations to the database.
docker-compose run web python manage.py migrate
- Initialize the seed data.
docker-compose run web python manage.py initdata
- Run the container. You may now use the API.
docker-compose up
curl -X POST http://localhost:8000/auth/signup/ \
-H "Content-Type: application/json" \
-d '{"username": "your_username", "email": "email@email.com", "password": "password"}'
curl -X POST http://localhost:8000/auth/login/ \
-H "Content-Type: application/json" \
-d '{"username": "your_username", "password": "password"}'
This returns something like {"refresh": "REFRESH_TOKEN_HERE", "access": "ACCESS_TOKEN_HERE"}
. You may use the access token for endpoints requiring authentication.
The token lasts for 1 day. You can get a new access token through supplying the refresh token to /auth/login/refresh/
.
curl -X POST http://localhost:8000/posts/ \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ACCESS_TOKEN_HERE" \
-d '{"title": "Post Title", "content": "Hello world!"}'
curl -X PUT http://localhost:8000/posts/POST_ID_HERE/ \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ACCESS_TOKEN_HERE" \
-d '{"title": "Updated Title", "content": "Updated content."}'
curl -L -X DELETE http://localhost:8000/posts/POST_ID_HERE/ \
-H "Authorization: Bearer ACCESS_TOKEN_HERE"
curl -L -X GET http://localhost:8000/me/posts/ \
-H "Authorization: Bearer ACCESS_TOKEN_HERE"
curl -L -X GET http://localhost:8000/posts/
curl -L -X GET http://localhost:8000/posts/1/
curl -X POST http://localhost:8000/posts/POST_ID_HERE/comments/ \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ACCESS_TOKEN_HERE" \
-d '{"content": "This is a comment."}'
curl -L -X DELETE http://localhost:8000/posts/POST_ID_HERE/comments/COMMENT_ID_HERE/ \
-H "Authorization: Bearer ACCESS_TOKEN_HERE"
curl -X GET http://localhost:8000/posts/POST_ID_HERE/comments/