A RESTful blog API built with Flask that allows for creating, reading, updating, and deleting blog posts, with advanced features like search and sorting.
Figure 1: Frontend interface for interacting with the Blog API
This project implements a complete blog management system with a Flask backend API and a simple frontend interface. It demonstrates RESTful API design principles and Client-Server architecture with cross-origin requests.
- RESTful API Endpoints: Full CRUD functionality for blog posts
- Search Capability: Filter posts by title or content
- Sorting: Order posts by title or content (ascending/descending)
- Error Handling: Proper HTTP status codes and error messages
- Frontend Interface: Simple UI for interacting with the API
Figure 2: Error handling interface for invalid requests
master_blog_api/
├── backend/
│ ├── backend_app.py # Main backend API with all endpoints
│ └── posts.py # Data store for blog posts
├── frontend/
│ ├── frontend_app.py # Frontend server
│ ├── static/
│ │ ├── main.js # Frontend JavaScript for API interaction
│ │ └── styles.css # CSS styling
│ └── templates/
│ └── index.html # Main HTML interface
├── assets/ # Documentation assets
│ ├── index.png # Frontend interface screenshot
│ └── error_handler.png # Error handling screenshot
├── requirements.txt # Python dependencies
└── README.md # This file
- Python 3.6 or higher
- Flask
- Flask-CORS (for cross-origin requests)
- Clone the repository:
git clone https://github.com/yourusername/blog-api.git
cd blog-api- Install the required packages:
pip install -r requirements.txt- Navigate to the backend directory:
cd backend- Run the backend server:
python backend_app.pyThe API will be available at http://localhost:5002/api
- Navigate to the frontend directory:
cd frontend- Run the frontend server:
python frontend_app.pyThe frontend will be available at http://localhost:5001
The frontend provides a simple interface to:
- View all posts
- Add new posts
- Delete posts
- Sort posts by title or content (ascending/descending)
For testing all API endpoints, including those not available in the frontend, you can use Postman:
- URL:
http://localhost:5002/api/posts - Optional query parameters:
sort: Field to sort by (titleorcontent)direction: Sort direction (ascordesc)
- Example:
GET http://localhost:5002/api/posts?sort=title&direction=desc
- URL:
http://localhost:5002/api/posts - Headers:
Content-Type: application/json - Body:
{ "title": "New Post Title", "content": "Post content goes here" } - Response: 201 Created with the new post data
- URL:
http://localhost:5002/api/posts/<id> - Headers:
Content-Type: application/json - Body:
{ "title": "Updated Title", "content": "Updated content" } - Response: 200 OK with the updated post data
- URL:
http://localhost:5002/api/posts/<id> - Response: 200 OK with success message
- URL:
http://localhost:5002/api/posts/search - Query parameters:
title: Search term for post titlescontent: Search term for post contents
- Example:
GET http://localhost:5002/api/posts/search?title=flask - Response: Posts matching the search criteria
Error handling can be tested by:
-
Missing Fields Error (400 Bad Request):
- Send a POST request without required fields:
curl -X POST http://localhost:5002/api/posts \ -H "Content-Type: application/json" \ -d '{"content": "Missing title field"}'
-
Not Found Error (404):
- Try to update or delete a non-existent post:
curl -X DELETE http://localhost:5002/api/posts/999
-
Invalid Sort Parameters (400 Bad Request):
- Request with invalid sort direction:
curl "http://localhost:5002/api/posts?sort=title&direction=invalid"
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/posts | List all posts |
| POST | /api/posts | Create a new post |
| PUT | /api/posts/{id} | Update a post |
| DELETE | /api/posts/{id} | Delete a post |
| GET | /api/posts/search | Search posts by title or content |
The backend code is thoroughly documented with clear function docstrings and code comments explaining the functionality of each endpoint and its error handling.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Happy coding! 🚀