Bird
0
0

After executing this Flask test client code:

medium📝 component behavior Q5 of 15
Flask - Testing Flask Applications
After executing this Flask test client code:
with app.test_client() as client:
    response = client.post('/greet', data={'user': 'Bob'})

Assuming the /greet route returns f"Welcome {request.form['user']}!", what will response.data contain?
Ab'Error: Missing user'
Bb'Welcome user!'
Cb'Welcome None!'
Db'Welcome Bob!'
Step-by-Step Solution
Solution:
  1. Step 1: Understand data parameter

    The data argument sends form data in the POST request.
  2. Step 2: Route response

    The route uses request.form['user'] which will be 'Bob' from the data sent.
  3. Step 3: Response data

    Flask returns a bytes object, so response.data will be b'Welcome Bob!'.
  4. Final Answer:

    b'Welcome Bob!' -> Option D
  5. Quick Check:

    Form data sent correctly, response bytes [OK]
Quick Trick: POST data sent as form, response is bytes [OK]
Common Mistakes:
MISTAKES
  • Expecting string instead of bytes in response.data
  • Using wrong key in form data
  • Assuming response.data contains JSON

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes