•  
      Internship #407351 4 Develop a Flask API to demonstrate GET, POST, PUT, PATCH, and DELETE methods with appropriate responses and status codes.
    #407351
    Arpita Gumma (arpita.gumma)
    2025-02-03 22:21
    2025-02-03 22:21
    Details
    4 Develop a Flask API to demonstrate GET, POST, PUT, PATCH, and DELETE methods with appropriate responses and status codes.

    Task: Write APIs to Demonstrate Various HTTP Methods

    Context: Develop APIs to showcase the functionality of different HTTP methods: GET, POST, PUT, PATCH, and DELETE. Ensure that each method returns appropriate response status codes and response bodies.

    from flask import Flask, request, jsonify

    app = Flask(name)

    Sample data stored in a dictionary

    users = {

    1: {"name": "Arpita", "age": 25},

    2: {"name": "Aditya", "age": 30} }

    1 GET - Retrieve all users

    @app.route('/users', methods=['GET'])

    def get_users():

    return jsonify(users), 200

    GET - Retrieve a specific user by ID

    @app.route('/users/int:user_id', methods=['GET'])

    def get_user(user_id):

    if user_id in users:

    return jsonify({user_id: users[user_id]}), 200
    

    return jsonify({"message": "User not found"}), 404

    2 POST - Add a new user

    @app.route('/users', methods=['POST'])

    def create_user():

    data = request.get_json()

    if "name" not in data or "age" not in data:

    return jsonify({"message": "Missing required fields"}), 400
    

    new_id = max(users.keys()) + 1 # Generate new user ID

    users[new_id] = {"name": data["name"], "age": data["age"]}

    return jsonify({new_id: users[new_id]}), 201

    3 PUT - Update an existing user (replace completely)

    @app.route('/users/int:user_id', methods=['PUT'])

    def update_user(user_id):

    data = request.get_json()

    if "name" not in data or "age" not in data:

    return jsonify({"message": "Missing required fields"}), 400
    

    if user_id in users:

    users[user_id] = {"name": data["name"], "age": data["age"]}
    
    return jsonify({user_id: users[user_id]}), 200
    

    return jsonify({"message": "User not found"}), 404

    4 PATCH - Partially update a user

    @app.route('/users/int:user_id', methods=['PATCH'])

    def patch_user(user_id):

    data = request.get_json()

    if user_id in users:

    users[user_id].update(data)  # Update only provided fields
    
    return jsonify({user_id: users[user_id]}), 200
    

    return jsonify({"message": "User not found"}), 404

    5 DELETE - Remove a user

    @app.route('/users/int:user_id', methods=['DELETE'])

    def delete_user(user_id):

    if user_id in users:

    del users[user_id]
    
    return jsonify({"message": "User deleted"}), 200
    

    return jsonify({"message": "User not found"}), 404

    if name == 'main':

    app.run(debug=True)

    1 Route : /users

    Method : GET

    Functionality : Returns all users as Json response.

    Response code : 200 ok

    Route : /users/<usre_id>

    Method : GET

    Functionality : fetches a user by user_id

    Response code :

     200 ok ( if user exists)
    
     400 not found (if the user id is missing)
    

    2 Route : /users

    Method : POST

    Functionality : Add a new user

    Steps:

     Extract Json request data
    
      Validate if "name" & "age" exist
    
      Assigns a new unique  user_id
    
     Adds the user to the users dictionary 
    

    Response code :

     200  ok (Created if successful)
    
     400  Bad request (if required  fields are missing)
    

    3 Route : /users/<usre_id>

    Method : PUT

    Functionality : full updates the users

    Steps:

    Checks if user_id is exists
    
    Validate input data
    
    Replace entire user record
    

    Response code :

    200 ok (if successful)
    
    404 not found (if user doesn't  exist)
    
    400  Bad request (if data is missing)
    

    4 Route : /users/<usre_id>

    Method : PATCH

    Functionality : Partially update user details

    Steps :

     check if user_id exists
    
     update only provided fields 
    

    Response code :

     200 ok (if successful)
    
     404 not found (if user doesn't exists)
    

    5 Route : /users/<usre_id>

    Method : DELETE

    Functionality : delete user from dictionary

    Response code :

     200 ok (if deleted  successful)
    
     404 not found (user not found)
    
    Empty
    Empty
    State of Progress
    2025-01-31
    Empty
    2025-02-03
    Arpita Gumma (arpita.gumma)
    Ajit kumar (ajit)
    2025-02-03
    Closed
    Attachments
    Empty
    References
    References list is empty