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?
PATCH updates part of a resource and usually returns the updated resource.
A successful PATCH request typically returns 200 OK with the updated resource in the response body. 201 Created is for new resources, 204 No Content means no body returned, and 400 Bad Request means the request was invalid.
Which reason best explains why PATCH is preferred over PUT when updating only some fields of a resource?
Think about what happens to fields not included in the request.
PATCH modifies only the fields sent in the request, leaving other fields unchanged. PUT replaces the entire resource, so missing fields are removed or reset.
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()What happens if the client sends an empty or invalid JSON body?
If request.json is None (no JSON sent), calling update() on None causes a TypeError. The code does not check if data is valid before updating.
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?
Remember JSON syntax rules for strings and commas.
Option B is valid JSON with a key-value pair. Option B has a trailing comma which is invalid in JSON. Option B uses => instead of colon. Option B misses quotes around the string value.
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?
PATCH replaces or sets fields sent, including null values.
The first PATCH updates the email field. The second PATCH sets the age field to null explicitly. PATCH does not remove fields unless the value is set to null or the server treats null as removal.