0% found this document useful (0 votes)
22 views5 pages

Restful Api & Flask Assignment

The document provides an overview of RESTful APIs and the Flask framework, detailing key concepts such as API specifications, routing, HTTP methods, and error handling. It includes code examples for creating a simple Flask application and RESTful API endpoints, as well as discussing database connections and security measures. Additionally, it highlights the importance of HTTP status codes and the role of Flask extensions like Flask-SQLAlchemy and Flask-RESTful in API development.

Uploaded by

ritikaagarwal508
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views5 pages

Restful Api & Flask Assignment

The document provides an overview of RESTful APIs and the Flask framework, detailing key concepts such as API specifications, routing, HTTP methods, and error handling. It includes code examples for creating a simple Flask application and RESTful API endpoints, as well as discussing database connections and security measures. Additionally, it highlights the importance of HTTP status codes and the role of Flask extensions like Flask-SQLAlchemy and Flask-RESTful in API development.

Uploaded by

ritikaagarwal508
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

RESTFUL API & FLASK ASSIGNMENT ​

THEORY
1.​ What is a RESTful API?​
- A RESTful API (Representational State Transfer API) is a web
service that follows REST principles, using standard HTTP
methods like GET, POST, PUT, and DELETE to enable
communication between clients and servers.​

2.​ Explain the concept of an API specification.​


An API specification defines how an API should behave,
including its endpoints, request and response formats,
authentication methods, and error handling. Examples include
OpenAPI (Swagger) and RAML.​

3.​ What is Flask, and why is it popular for building APIs?​


Flask is a lightweight Python web framework that makes it easy to
build web applications and APIs. It’s popular due to its simplicity,
flexibility, and large ecosystem of extensions.​

4.​ What is routing in Flask?​


Routing in Flask maps URLs to functions, determining how
different URL patterns trigger specific logic in a Flask app.​

5.​ How do you create a simple Flask application?​



from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
return "Hello, Flask!"
if __name__ == '__main__':
app.run(debug=True)

6.​ What are HTTP methods used in RESTful APIs?​

○​ GET – Retrieve data​

○​ POST – Create new data​

○​ PUT – Update existing data​

○​ DELETE – Remove data​

○​ PATCH – Partially update data​

7.​ What is the purpose of the @app.route() decorator in


Flask?​
It binds a URL to a specific function, allowing Flask to handle
requests to that route.​

8.​ What is the difference between GET and POST HTTP


methods?​

○​ GET: Retrieves data, does not modify the server state.​

○​ POST: Submits data to the server, often used for creating resources.​

9.​ How do you handle errors in Flask APIs?​


Use error handlers like:​

From flask import jsonify

@app.errorhandler(404)
def not_found(error):
return jsonify({"error": "Not Found"}), 404
10.​ How do you connect Flask to a SQL database?​
Use Flask-SQLAlchemy:​

from flask_sqlalchemy import SQLAlchemy

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
db = SQLAlchemy(app)

11.​What is the role of Flask-SQLAlchemy?​


It is an ORM (Object Relational Mapper) that simplifies database
interactions in Flask.​

12.​What are Flask blueprints, and how are they useful?​


Blueprints allow modular structuring of a Flask app by grouping
related routes, making the app more scalable.​

13.​What is the purpose of Flask's request object?​


The request object provides access to request data such as form
data, JSON payloads, and headers.​

14.​How do you create a RESTful API endpoint using Flask?​



from flask import Flask, jsonify

app = Flask(__name__)

@app.route('/api/data', methods=['GET'])
def get_data():
return jsonify({"message": "Hello, API!"})

if __name__ == '__main__':
app.run()

15.​What is the purpose of Flask’s jsonify() function?​


It formats Python dictionaries/lists into JSON responses.​

16.​Explain Flask’s url_for() function.​


It generates URLs dynamically for view functions:​

url_for('home')

17.​How does Flask handle static files (CSS, JavaScript, etc.)?​


Flask serves static files from the static/ directory using
/static/<filename>.​

18.​What is an API specification, and how does it help in building a


Flask API?​
An API specification (e.g., OpenAPI) defines expected
request/response formats, improving consistency and
documentation.​

19.​What are HTTP status codes, and why are they important in a
Flask API?​
They indicate request outcomes (e.g., 200 OK, 404 Not
Found, 500 Internal Server Error).​

20.​How do you handle POST requests in Flask?​



from flask import request

@app.route('/submit', methods=['POST'])
def submit():
data = request.json
return jsonify(data), 201
21.​How would you secure a Flask API?​

○​ Use authentication (JWT, OAuth).​

○​ Validate input data.​

○​ Enable HTTPS.​

○​ Implement rate limiting.​

22.​What is the significance of the Flask-RESTful extension?​


It simplifies the creation of REST APIs with class-based views
and request parsing.​

23.​What is the role of Flask’s session object?​


It stores user session data across requests using cookies.

You might also like