This is a simple example of a REST API built with Python and Flask. The API provides endpoints to perform CRUD (Create, Read, Update, Delete) operations on programming languages.
- Python 3.x
- Flask (Install using
pip install Flask
)
- Clone the repository:
git clone <repository_url>
cd <repository_name>
- Install Flask:
pip install Flask
- Run the Flask app:
python app.py
The server should start running on http://127.0.0.1:5000/
.
- URL:
/programming_languages
- Method: GET
- Description: Retrieve a list of all programming languages.
- URL:
/programming_languages
- Method: POST
- Description: Create a new programming language.
- URL:
/programming_languages/<lang_name>
- Method: GET
- Description: Retrieve information about a specific programming language.
- URL:
/programming_languages/<lang_name>
- Method: PUT
- Description: Update information for a specific programming language.
- URL:
/programming_languages/<lang_name>
- Method: DELETE
- Description: Delete a specific programming language.
The API accepts JSON data for POST and PUT requests. The response will also be in JSON format.
{
"name": "Python",
"year_invented": 1991,
"creator": "Guido van Rossum"
}
{
"name": "Python",
"year_invented": 1991,
"creator": "Guido van Rossum"
}
[
{
"name": "Python",
"year_invented": 1991,
"creator": "Guido van Rossum"
},
{
"name": "JavaScript",
"year_invented": 1995,
"creator": "Brendan Eich"
},
...
]
{
"message": "Programming language deleted successfully."
}
In case of errors, the API will return appropriate HTTP status codes and error messages in the response.
Here are some sample cURL commands to interact with the API:
curl -X POST -H "Content-Type: application/json" -d '{"name":"Java", "year_invented": 1995, "creator": "James Gosling"}' http://127.0.0.1:5000/programming_languages
curl http://127.0.0.1:5000/programming_languages/Java
curl -X PUT -H "Content-Type: application/json" -d '{"year_invented": 1996}' http://127.0.0.1:5000/programming_languages/Java
curl -X DELETE http://127.0.0.1:5000/programming_languages/Java
This project is licensed under the MIT License. Feel free to modify and use it for your own projects.
Please note that this is a basic example for learning purposes and not intended for production use. In a real-world scenario, you would need to add proper error handling, input validation, authentication, and database integration to ensure security and reliability.