0
0
Flaskframework~10 mins

Why APIs matter in Flask - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a basic Flask API endpoint that returns a greeting message.

Flask
from flask import Flask
app = Flask(__name__)

@app.route('/hello')
def hello():
    return [1]
Drag options to blanks, or click blank then click option'
A"Hello, API!"
Bprint("Hello, API!")
Creturn Hello, API!
Dresponse("Hello, API!")
Attempts:
3 left
💡 Hint
Common Mistakes
Using print instead of return
Not using quotes around the string
2fill in blank
medium

Complete the code to import the Flask class correctly.

Flask
[1] Flask
app = Flask(__name__)
Drag options to blanks, or click blank then click option'
Afrom flask import
Bfrom flask
Cimport
Dinclude
Attempts:
3 left
💡 Hint
Common Mistakes
Using only 'import flask'
Using 'include' which is not Python syntax
3fill in blank
hard

Fix the error in the route decorator to correctly define the API endpoint.

Flask
@app.route([1])
def greet():
    return "Hi there!"
Drag options to blanks, or click blank then click option'
Agreet
B/greet
C'/greet'
D"greet"
Attempts:
3 left
💡 Hint
Common Mistakes
Missing quotes around the path
Not starting the path with a slash
4fill in blank
hard

Fill both blanks to create a JSON response from the API using Flask's jsonify.

Flask
from flask import Flask, [1]

app = Flask(__name__)

@app.route('/data')
def data():
    return [2]({'message': 'Hello, JSON!'})
Drag options to blanks, or click blank then click option'
Ajsonify
Bjson
Cmake_response
DResponse
Attempts:
3 left
💡 Hint
Common Mistakes
Importing json instead of jsonify
Using make_response without jsonify
5fill in blank
hard

Fill all three blanks to create a Flask API that accepts a POST request and returns the posted JSON data.

Flask
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route('/postdata', methods=[[1]])
def post_data():
    data = request.[2]
    return jsonify([3])
Drag options to blanks, or click blank then click option'
A"POST"
Bjson
Cdata
DGET
Attempts:
3 left
💡 Hint
Common Mistakes
Using GET instead of POST
Accessing request.data instead of request.json
Returning raw data without jsonify