300-435 · Question #159
Refer to the exhibit. Which code snippet must be added to the box in the code to complete the webhook receiver function?
The correct answer is B. `@app.route('/webhook', methods=['POST']) @basic_auth.required def webhook(): if request.method == 'POST': print('Webhook Received') request_json = request.json`. A valid Flask webhook receiver requires a route decorator, an auth decorator, and a properly defined Python function using the def keyword.
Question
Options
- A
@app.route('/webhook', methods=['POST']) @basic_auth.required if request.method == 'POST': print('Webhook Received') request_json = request.json - B
@app.route('/webhook', methods=['POST']) @basic_auth.required def webhook(): if request.method == 'POST': print('Webhook Received') request_json = request.json - C
@app.route('/webhook', methods=['PUT']) @basic_auth.required import webhook() if request.method == 'POST': print('Webhook Received') request_json = request.json - D
@app.route('/webhook', methods=['PUT']) @basic_auth.required class webhook(): if request.method == 'POST': print('Webhook Received') request_json = request.json
How the community answered
(24 responses)- B88% (21)
- C8% (2)
- D4% (1)
Why each option
A valid Flask webhook receiver requires a route decorator, an auth decorator, and a properly defined Python function using the def keyword.
This option is missing the def webhook(): function definition line - without it the indented code block has no function to belong to, making it a syntax error.
Option B correctly defines a Flask route using @app.route('/webhook', methods=['POST']), applies basic auth protection with @basic_auth.required, and then defines a proper Python function with def webhook():. The function body then checks the request method and parses the incoming JSON payload. All three elements - route decorator, auth decorator, and function definition - are required for a functional Flask endpoint.
Using methods=['PUT'] instead of POST is incorrect for a webhook receiver, and import webhook() is invalid Python syntax - you cannot import a function with parentheses.
methods=['PUT'] is wrong for a webhook that expects POST requests, and using class webhook(): defines a class instead of a function, which is incorrect for a Flask route handler.
Concept tested: Flask webhook route and function definition syntax
Source: https://flask.palletsprojects.com/en/stable/api/#flask.Flask.route
Topics
Community Discussion
No community discussion yet for this question.