0
0
Rest APIprogramming~20 mins

409 Conflict in Rest API - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Conflict Resolver
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:30remaining
What is the HTTP status code returned?

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?

Rest API
HTTP/1.1 409 Conflict
Content-Type: application/json

{"error": "Conflict detected: resource has been modified."}
A409 Conflict
B404 Not Found
C200 OK
D500 Internal Server Error
Attempts:
2 left
💡 Hint

Think about which status code means the request conflicts with the current state of the resource.

🧠 Conceptual
intermediate
1:30remaining
Why use 409 Conflict instead of 400 Bad Request?

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?

ABecause 400 Bad Request indicates the server is down.
BBecause 400 Bad Request means the client sent malformed syntax, not a conflict.
CBecause 409 Conflict means the server accepted the request without issues.
DBecause 400 Bad Request is only for authentication errors.
Attempts:
2 left
💡 Hint

Think about the difference between syntax errors and resource state conflicts.

Predict Output
advanced
2:00remaining
What is the output of this conflict handling code?

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?

Rest API
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
A200 OK with updated resource data
B404 Not Found
C409 Conflict with error message 'Version conflict'
D500 Internal Server Error
Attempts:
2 left
💡 Hint

Check the version comparison logic in the code.

🔧 Debug
advanced
2:30remaining
Identify the bug causing incorrect status code

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?

Rest API
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]);
});
AMissing return statements after res.status calls causing multiple responses
BIncorrect comparison operator used for version check
Cresources object is not defined properly
DMiddleware for parsing JSON is missing
Attempts:
2 left
💡 Hint

Check what happens after sending a response with res.status().json()

🚀 Application
expert
1:00remaining
How many items are in the conflict error response list?

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?

A4
B2
C1
D3
Attempts:
2 left
💡 Hint

Count the number of objects inside the "conflicts" array.