# Activity 39: Python Flask Cookie

Implementation of a Flask application that handles cookies using both POST and GET methods as per the Activity 39 requirements.

### Implementing Cookies in Flask

#### Step 1: Set Up Your Flask Project

1. **Create a New Project Folder**:
    
    ```plaintext
     mkdir nicolas_python_flask_cookies
     cd nicolas_python_flask_cookies
    ```
    
2. **Set Up a Virtual Environment**:
    
    ```plaintext
     python -m venv venv
     venv\Scripts\activate  # For Windows users
     pip install Flask
    ```
    
3. **Create the Main Application File**: Create a file named [`app.py`](http://app.py/) and add the following code:
    
    ```plaintext
     from flask import Flask, request, make_response, jsonify
    
     app = Flask(__name__)
    
     @app.route('/setcookies', methods=['POST'])
     def set_cookies():
         # Get the cookie value from the POST request
         cookie_value = request.form.get('cookie_value')
         response = make_response(jsonify({"message": "Cookie has been set!"}))
         # Set the cookie
         response.set_cookie('my_cookie', cookie_value)
         return response
    
     @app.route('/getcookies', methods=['GET'])
     def get_cookies():
         # Retrieve the cookie from the request
         cookie_value = request.cookies.get('my_cookie')
         if cookie_value:
             return jsonify({"cookie_value": cookie_value})
         return jsonify({"message": "No cookie found!"})
    
     if __name__ == '__main__':
         app.run(debug=True)
    ```
    

#### Step 2: Test Your Application

1. **Run the Flask Application**: Execute the following command in your terminal:
    
    ```plaintext
     python app.py
    ```
    
2. **Setting the Cookie**: You can use a tool like Postman.
    
    Set method to `POST`
    
    ```plaintext
     http://127.0.0.1:5000/setcookies -d "cookie_value=your_cookie_value_here"
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1730548006511/cacc7f57-3ca6-41d5-a224-809ef4dd1b97.png?auto=compress,format&format=webp align="left")
    
3. **Getting the Cookie**: To retrieve the cookie, send a GET request:
    
    ```plaintext
     http://127.0.0.1:5000/getcookies
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1730548359803/8b9544b0-bdd0-49f7-903b-582610e01629.png?auto=compress,format&format=webp align="left")
    

#### Step 3: Initialize Git and Push to GitHub

```plaintext
git init
git add .
git commit -m "Initial commit for Python Flask cookie implementation"
git remote add origin https://github.com/MonetForProgrammingPurposes/nicolas_python_flask_cookies.git
git branch -M master
git push -u origin master
```
