0
0
Rest APIprogramming~10 mins

400 Bad Request in Rest API - Interactive Code Practice

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

Complete the code to return a 400 Bad Request status in a REST API response.

Rest API
return Response(status=[1])
Drag options to blanks, or click blank then click option'
A500
B200
C400
D404
Attempts:
3 left
💡 Hint
Common Mistakes
Using 200 which means OK instead of 400.
Using 404 which means Not Found.
Using 500 which means Internal Server Error.
2fill in blank
medium

Complete the code to check if the request JSON is missing a required field and return 400 Bad Request.

Rest API
if 'name' not in request.json:
    [1] 'Missing name field', 400
Drag options to blanks, or click blank then click option'
Areturn
Braise
Cabort
Dprint
Attempts:
3 left
💡 Hint
Common Mistakes
Using abort without importing it.
Using raise which is for exceptions.
Using print which only outputs to console.
3fill in blank
hard

Fix the error in the code to correctly return a 400 Bad Request with a JSON error message.

Rest API
return jsonify(error='Invalid input'), [1]
Drag options to blanks, or click blank then click option'
A200
B400
C404
D500
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 200 which means success.
Returning 404 which means Not Found.
Returning 500 which means server error.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that filters keys with empty values and returns 400 if any are found.

Rest API
errors = {key: value for key, value in data.items() if value [1] ''}
if errors:
    return jsonify(errors), [2]
Drag options to blanks, or click blank then click option'
A==
B400
C!=
D200
Attempts:
3 left
💡 Hint
Common Mistakes
Using != instead of == in the comprehension.
Returning status 200 instead of 400.
5fill in blank
hard

Fill all three blanks to validate input length and return 400 Bad Request if invalid.

Rest API
if len([1]) [2] [3]:
    return jsonify(error='Input too short'), 400
Drag options to blanks, or click blank then click option'
Ainput_data
B<
C5
Dinput
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable name like 'input_data'.
Using > instead of < for length check.
Using a wrong number for length.