Skip to content

A simple REST API for user management using Flask, supporting CRUD operations via POST, GET, PUT, DELETE.

Notifications You must be signed in to change notification settings

Rohith-a441/task4-rest-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

task4-rest-api


πŸš€ Task 4: Building a REST API with Flask

πŸ“Œ Objective

The objective of this task is to create a RESTful API using Python's Flask framework that performs CRUD (Create, Read, Update, Delete) operations on user data. The API uses in-memory storage (a simple list) for managing users and supports HTTP methods: POST, GET, PUT, and DELETE.


πŸ›  Tools Used

  • Language: Python
  • Framework: Flask
  • API Testing Tool: Postman
  • IDE: VS Code
  • Platform: Localhost (127.0.0.1:5000)

πŸ“‚ Project Structure

task-4-rest-api/ β”œβ”€β”€ app.py # Flask application β”œβ”€β”€ README.md └── screenshots/ # Postman API testing screenshots β”œβ”€β”€ post-create-user.png β”œβ”€β”€ get-all-users.png β”œβ”€β”€ put-update-user.png └── delete-user.png


πŸ”₯ Features

  • Add new users using POST /users
  • Get a list of all users using GET /users
  • Update a specific user by ID using PUT /users/
  • Delete a user by ID using DELETE /users/
  • JSON responses with meaningful status codes
  • Handles both lowercase and capitalized JSON keys ("name" or "Name")

βš™ How to Run This Project

  1. Open terminal and install Flask:

pip install flask

  1. Run the Flask app:

python app.py

  1. Open Postman and test the API using:

http://127.0.0.1:5000/


🧠 Code Explanation (Line by Line)

Importing Modules:
We start by importing Flask and its required classes: Flask, request, and jsonify to create and handle web routes and responses.

Initializing App:
We create a Flask app instance using app = Flask(name).

In-Memory Database:
We define a Python list users = [] which stores user dictionaries containing id, name, and email.


POST /users – Add New User

  • This route accepts JSON data via POST.
  • It checks for either lowercase or capitalized keys (name or Name, email or Email).
  • If either field is missing, it returns a 400 Bad Request.
  • Otherwise, it creates a user, assigns an incremental ID, and appends it to the list.
  • Returns 201 Created with user data.

GET /users – Get All Users

  • Returns the entire list of users.
  • Responds with 200 OK.

GET /users/ – Get User by ID

  • Accepts a user ID as a path parameter.
  • Searches for the user with that ID.
  • If found, returns the user data.
  • If not found, returns 404 Not Found.

PUT /users/ – Update User

  • Accepts user ID and JSON payload.
  • Checks if the user exists.
  • Updates name or email if present in the payload (case-insensitive).
  • Returns updated user with 200 OK.

DELETE /users/ – Delete User

  • Accepts user ID as a parameter.
  • Removes the user from the list if they exist.
  • Responds with confirmation message and 200 OK.

βœ… API Endpoints Summary

Method Endpoint Description
POST /users Create a new user
GET /users Get all users
GET /users/ Get a user by ID
PUT /users/ Update user by ID
DELETE /users/ Delete user by ID

πŸ“Έ Screenshots

All API requests were successfully tested in Postman.

Method Screenshot
βœ… POST - Create User POST
βœ… GET - All Users GET
βœ… PUT - Update User PUT
βœ… DELETE - Remove User DELETE

Each screenshot clearly displays:

  • Correct request URL and method
  • Proper headers and JSON body
  • Successful status codes (200 OK, 201 Created)
  • Valid JSON responses

Conclusion

  • πŸ’‘ Fully working REST API using Flask
  • πŸ”„ Supports all HTTP methods with complete CRUD functionality
  • πŸ’¬ User-friendly error messages and validations
  • 🧠 Accepts both name/Name and email/Email inputs for flexibility
  • πŸ“Έ Well-documented Postman results with proof screenshots
  • 🧾 Professionally written README.md with full explanations
  • πŸ’» Clean, maintainable, and scalable Python code

πŸ‘¨β€πŸ’» Developer Info

Name: Rohith K N
Internship: Python Development (1 Month)
Task Number: Task 4 - REST API using Flask
Date: June 2025
Goal: Top 10 out of 200 interns ✨


About

A simple REST API for user management using Flask, supporting CRUD operations via POST, GET, PUT, DELETE.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages