Rust-URL-Shortening

API Documentation

Base URL

http://localhost:8080

Endpoints

1. Create Short URL

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:


2. List All URLs

Retrieves 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
  }
]

3. Redirect to Original URL

Redirects to the original URL using the short code.

Endpoint: GET /{short_code}

Response: 302 Found

Error Responses:


4. Get URL Statistics

Retrieves 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:


Error Format

All error responses follow this format:

{
  "error": "Error description message"
}

Rate Limiting

Currently, there is no rate limiting implemented. Consider implementing rate limiting for production use.

Authentication

Currently, the API doesn’t require authentication. For production deployment, consider implementing:

Examples

Using cURL

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

Using Python

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())

Using JavaScript (fetch)

// 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));