This project is a URL shortener API built using Django Rest Framework. The API allows users to submit long URLs and receive shorter versions that are mapped to the original URLs through a database. It includes validation to ensure that only valid URLs are submitted, and it prevents duplicate URLs from being stored. Additionally, the project utilizes django-cors-headers
to manage and verify access to the API from different origin URLs.
- Shorten long URLs into easily shareable short URLs.
- Prevent duplicate URLs from being stored in the database.
- Validate submitted URLs to ensure they are in the correct format.
- Manage and control cross-origin resource sharing (CORS) using
django-cors-headers
.
-
Clone the repository:
git clone https://github.com/edgarmuyomba/urlShortenerAPI.git cd urlShortenerAPI
-
Create and activate a virtual environment
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install the required packages
pip install -r requirements.txt
-
Setup the database
python manage.py runserver
-
Access the API at
http://127.0.0.1:8000/
in your browser or API client.
POST /api/shorten/
- Description: Submit a URL to be shortened.
- Request Body:
{ "source": "https://www.example.com/long-url" }
- Reponse
{ "tiny": "http://127.0.0.1:8000/<int>/" }
GET /s/{short_code}
- Description: Redirect to the original URL associated with the provided short code.
The project uses django-cors-headers
to manage cross-origin resource sharing. By default, the CORS configuration allows requests from all origins. You can modify the CORS_ALLOW_ALL_ORIGINS
setting in settings.py to restrict access to specific origins
CORS_ALLOW_ALL_ORIGINS = False
CORS_ALLOWED_ORIGINS = [
"http://localhost:8001",
"https://your-frontend-domain.com",
]