0
0
Rest APIprogramming~10 mins

Batch create endpoint design 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 define the HTTP method for a batch create endpoint.

Rest API
app.[1]('/items/batch', create_items_batch)
Drag options to blanks, or click blank then click option'
ADELETE
BPUT
CPOST
DGET
Attempts:
3 left
💡 Hint
Common Mistakes
Using GET instead of POST for creating resources.
Using DELETE or PUT which are for other operations.
2fill in blank
medium

Complete the code to accept a list of items in the request body for batch creation.

Rest API
def create_items_batch():
    items = request.[1]
Drag options to blanks, or click blank then click option'
Aargs
Bjson
Cform
Dheaders
Attempts:
3 left
💡 Hint
Common Mistakes
Using request.args which reads query parameters instead of body.
Using request.form which is for form data, not JSON.
3fill in blank
hard

Fix the error in the batch create function to correctly iterate over the items list.

Rest API
def create_items_batch():
    items = request.json
    for [1] in items:
        save_item(item)
    return {'status': 'success'}
Drag options to blanks, or click blank then click option'
Aitems
Bdata
Ci
Ditem
Attempts:
3 left
💡 Hint
Common Mistakes
Using the list variable name as the loop variable.
Using an undefined variable inside the loop.
4fill in blank
hard

Fill both blanks to validate each item has a 'name' key and return error if missing.

Rest API
def create_items_batch():
    items = request.json
    for item in items:
        if [1] not in item:
            return [2]
    return {'status': 'success'}
Drag options to blanks, or click blank then click option'
A'name'
B{'error': 'Missing name'}
C'id'
D{'error': 'Invalid data'}
Attempts:
3 left
💡 Hint
Common Mistakes
Checking wrong keys like 'id' instead of 'name'.
Returning a generic error message instead of specific.
5fill in blank
hard

Fill all three blanks to create a batch endpoint that saves items and returns count of created items.

Rest API
def create_items_batch():
    items = request.[1]
    count = 0
    for [2] in items:
        save_item([3])
        count += 1
    return {'created': count}
Drag options to blanks, or click blank then click option'
Ajson
Bitem
Dargs
Attempts:
3 left
💡 Hint
Common Mistakes
Using request.args instead of request.json.
Using different variable names inconsistently.