http://localhost:8080
Creates a new shortened URL.
Endpoint: POST /
Request Headers:
Content-Type: application/json
Request Body:
{
"original_url": "https://example.com/very/long/url"
}
Response: 200 OK
{
"id": 1,
"original_url": "https://example.com/very/long/url",
"short_code": "abc123",
"created_at": "2024-01-15T10:30:00Z",
"expires_at": null
}
Error Responses:
400 Bad Request - Invalid URL format500 Internal Server Error - Database errorRetrieves a list of all shortened URLs.
Endpoint: GET /
Response: 200 OK
[
{
"id": 1,
"original_url": "https://example.com/page1",
"short_code": "abc123",
"created_at": "2024-01-15T10:30:00Z",
"expires_at": null
},
{
"id": 2,
"original_url": "https://example.com/page2",
"short_code": "def456",
"created_at": "2024-01-15T11:00:00Z",
"expires_at": null
}
]
Redirects to the original URL using the short code.
Endpoint: GET /{short_code}
Response: 302 Found
Location: https://example.com/original/urlError Responses:
404 Not Found - Short code doesn’t exist or has expiredRetrieves statistics for a shortened URL.
Endpoint: GET /stats/{short_code}
Response: 200 OK
{
"short_code": "abc123",
"original_url": "https://example.com/page",
"click_count": 42,
"created_at": "2024-01-15T10:30:00Z",
"last_accessed": "2024-01-16T15:45:00Z"
}
Error Responses:
404 Not Found - Short code doesn’t existAll error responses follow this format:
{
"error": "Error description message"
}
Currently, there is no rate limiting implemented. Consider implementing rate limiting for production use.
Currently, the API doesn’t require authentication. For production deployment, consider implementing:
Create a short URL:
curl -X POST http://localhost:8080/ \
-H "Content-Type: application/json" \
-d '{"original_url": "https://example.com"}'
List all URLs:
curl http://localhost:8080/
Test redirection:
curl -I http://localhost:8080/abc123
import requests
# Create short URL
response = requests.post(
'http://localhost:8080/',
json={'original_url': 'https://example.com'}
)
print(response.json())
# List all URLs
response = requests.get('http://localhost:8080/')
print(response.json())
// Create short URL
fetch('http://localhost:8080/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
original_url: 'https://example.com'
})
})
.then(response => response.json())
.then(data => console.log(data));
// List all URLs
fetch('http://localhost:8080/')
.then(response => response.json())
.then(data => console.log(data));