0
0
Rest APIprogramming~20 mins

PATCH for partial updates in Rest API - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PATCH Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:00remaining
What is the response status code after a successful PATCH request?

Consider a REST API where you send a PATCH request to update a user's email partially. The server successfully updates the email field.

What is the typical HTTP status code returned by the server?

A200 OK
B201 Created
C204 No Content
D400 Bad Request
Attempts:
2 left
💡 Hint

PATCH updates part of a resource and usually returns the updated resource.

🧠 Conceptual
intermediate
1:30remaining
Why use PATCH instead of PUT for partial updates?

Which reason best explains why PATCH is preferred over PUT when updating only some fields of a resource?

APATCH always returns 201 Created, PUT does not.
BPATCH requires sending the full resource, PUT only partial data.
CPATCH updates only specified fields without affecting others, while PUT replaces the entire resource.
DPATCH is faster because it uses GET method internally.
Attempts:
2 left
💡 Hint

Think about what happens to fields not included in the request.

🔧 Debug
advanced
2:00remaining
Identify the error in this PATCH request handler code

Given this Python Flask code snippet handling a PATCH request, what error will occur?

from flask import Flask, request, jsonify
app = Flask(__name__)

users = {1: {'name': 'Alice', 'email': 'alice@example.com'}}

@app.route('/users/<int:user_id>', methods=['PATCH'])
def update_user(user_id):
    data = request.json
    if user_id not in users:
        return jsonify({'error': 'User not found'}), 404
    users[user_id].update(data)
    return jsonify(users[user_id]), 200

if __name__ == '__main__':
    app.run()
ATypeError if request.json is None because no JSON was sent
BKeyError if user_id is not in users
CSyntaxError due to incorrect route decorator
DNo error, code runs correctly
Attempts:
2 left
💡 Hint

What happens if the client sends an empty or invalid JSON body?

📝 Syntax
advanced
1:00remaining
Which PATCH request JSON body is valid for partial update?

You want to update only the email field of a user via PATCH. Which JSON body is syntactically correct and valid for this partial update?

A{"email": "newemail@example.com",}
B{"email": "newemail@example.com"}
C{"email" => "newemail@example.com"}
D{"email": newemail@example.com}
Attempts:
2 left
💡 Hint

Remember JSON syntax rules for strings and commas.

🚀 Application
expert
2:30remaining
What is the final user data after this PATCH sequence?

Given the initial user data:

{"name": "Bob", "email": "bob@example.com", "age": 30}

And these two PATCH requests applied in order:

PATCH 1 body: {"email": "bob.new@example.com"}
PATCH 2 body: {"age": null}

What is the final user data after both PATCH requests?

A{"name": "Bob", "email": "bob@example.com", "age": 30}
B{"name": "Bob", "email": "bob.new@example.com", "age": 0}
C{"name": "Bob", "email": "bob.new@example.com"}
D{"name": "Bob", "email": "bob.new@example.com", "age": null}
Attempts:
2 left
💡 Hint

PATCH replaces or sets fields sent, including null values.