Consider a REST API endpoint that updates a user profile. If two clients try to update the same user data simultaneously, the server detects a conflict and returns a 409 Conflict status code.
What will be the HTTP status code in the response when this conflict occurs?
HTTP/1.1 409 Conflict Content-Type: application/json {"error": "Conflict detected: resource has been modified."}
Think about which status code means the request conflicts with the current state of the resource.
The 409 Conflict status code indicates that the request could not be completed due to a conflict with the current state of the target resource. This is commonly used when multiple clients try to update the same resource simultaneously.
When a client sends a request that conflicts with the current state of a resource, why is it better to respond with a 409 Conflict status code rather than a 400 Bad Request?
Think about the difference between syntax errors and resource state conflicts.
400 Bad Request means the request syntax is invalid or malformed. 409 Conflict means the request is syntactically valid but cannot be processed because it conflicts with the resource's current state.
Given the following Python Flask code snippet handling a PUT request to update a resource, what will be the HTTP status code returned if the resource version does not match?
from flask import Flask, request, jsonify app = Flask(__name__) resources = {"item1": {"version": 1, "data": "old"}} @app.route('/resource/<id>', methods=['PUT']) def update_resource(id): if id not in resources: return jsonify({"error": "Not found"}), 404 client_version = request.json.get('version') if client_version != resources[id]['version']: return jsonify({"error": "Version conflict"}), 409 resources[id]['data'] = request.json.get('data') resources[id]['version'] += 1 return jsonify(resources[id]), 200 # Assume a PUT request with JSON {"version": 0, "data": "new"} to /resource/item1
Check the version comparison logic in the code.
The code checks if the client's version matches the current resource version. If not, it returns 409 Conflict with an error message.
In this Node.js Express code, the server should return 409 Conflict when a resource update conflicts, but it always returns 200 OK. What is the bug?
const express = require('express'); const app = express(); app.use(express.json()); const resources = { item1: { version: 1, data: 'old' } }; app.put('/resource/:id', (req, res) => { const id = req.params.id; if (!resources[id]) { return res.status(404).json({ error: 'Not found' }); } const clientVersion = req.body.version; if (clientVersion !== resources[id].version) { return res.status(409).json({ error: 'Version conflict' }); } resources[id].data = req.body.data; resources[id].version += 1; res.status(200).json(resources[id]); });
Check what happens after sending a response with res.status().json()
Without return statements after sending a response, the function continues and sends another response, causing the last one (200 OK) to be sent always.
A REST API returns a 409 Conflict response with a JSON body listing multiple conflicting fields as follows:
{
"error": "Conflict detected",
"conflicts": [
{"field": "email", "message": "Email already in use"},
{"field": "username", "message": "Username taken"},
{"field": "phone", "message": "Phone number conflict"}
]
}How many conflict items are in the "conflicts" list?
Count the number of objects inside the "conflicts" array.
The "conflicts" array contains three objects, each describing a conflicting field.