This project demonstrates how to create a simple REST API using Flask in Python and interact with it using a Java client that sends HTTP requests.
.
├── app.py # Flask API server (Python)
├── RequestSender.java # Java client to send HTTP requests
├── requirements.txt # List of Python dependencies
├── .venv # Virtual environment (not to be pushed to Git)
└── README.md # Project documentation
The Flask API (app.py
) is a simple web server that listens for HTTP GET and POST requests. The API has one endpoint:
GET /api/ Returns a simple "Request received (Hello world)" message.
To run the Flask API, follow these steps:
-
Create a Python virtual environment (if you haven't already):
python -m venv .venv
-
Activate the virtual environment:
-
Windows:
.\.venv\Scripts\activate
-
Mac/Linux:
source .venv/bin/activate
-
-
Install the required dependencies:
pip install -r requirements.txt
-
Run the Flask app:
python app.py
-
The Flask app will now be running on
http://0.0.0.0:8765/
. You can test it by visitinghttp://127.0.0.1:8765/api/
in your browser or by sending an HTTP request using the Java client.
The Java code (RequestSender.java
) sends an HTTP GET request to the Flask API and prints the response received.
-
Compile the Java code:
javac RequestSender.java
-
Run the Java client:
java RequestSender
-
The output should display the response from the Flask API:
Response from server: Request received (Hello world)
This file lists all the required Python dependencies for the Flask API.
Flask==2.1.0