Trending

World technology news

→
Describing a closed-report conflict with HTTP Problem Details
Software Engineering · 2016
Software Engineering2016

Describing a closed-report conflict with HTTP Problem Details

Explore the problem type and occurrence identifiers from RFC 7807 in a local HTTP response. Build a 409 error that preserves HTTP status semantics and gives the client a specific explanation.

Archive retrospective · Topic year 2016

Developer's JournalPublished: Dec 5, 20266 min read
Reading progress0%
In this article
  1. Two close requests produce different results
  2. A stable type and an individual occurrence
  3. Exercising the response on loopback
  4. What a client observes at each layer
  5. What to communicate and what to keep internal
  6. Using the historical design today

Two close requests produce different results

Two operators see the same report as open. The first sends a close request and the operation succeeds. The second uses the stale view and presses the same button. The server now sees a closed report. It can express that conflict using HTTP 409. The status is also available to other operations, such as a create request that would reuse an existing name. A client that only sees the code cannot identify which condition applies. RFC 7807, published in March 2016, described a shared format for machine-readable HTTP error details. The report example uses it to explain the current state of a particular report while preserving the general HTTP outcome.

A stable type and an individual occurrence

The example uses a type URI ending in report-already-closed. A production API should control the namespace and document what its types mean. The host example.com in this demonstration is only a placeholder. The type can remain the same when a different report is affected. The instance field can identify the individual occurrence through a separate URI reference. The human-oriented title gives a brief description and detail says report 27 had already been closed. A translated title may use different words while the type stays stable. Clients can use that identifier to choose the right recovery action even when several API problems use the same HTTP status.

Service architectureConceptual sketch · summary of the article's technical flow

Exercising the response on loopback

The Node.js server starts on the loopback address and stores whether the report is closed in process memory. The first POST request changes the state and the API returns 204 with no body. The second POST uses the same URL and encounters the closed state. The server sends 409 and application/problem+json. Assertions check both status codes, the empty response, the media type and selected fields in the problem. The server closes at the end and a fresh process restores the initial state. This is a current HTTP client and runtime demonstrating the 2016 format. It does not require an old framework installation or an external service.

import assert from 'node:assert/strict';
import { createServer } from 'node:http';
import { once } from 'node:events';

const conflict = 'https://example.com/problems/report-already-closed';
let closed = false;
const server = createServer((request, response) => {
  if (request.method !== 'POST' || request.url !== '/reports/27/close') {
    response.writeHead(404).end();
    return;
  }
  if (!closed) {
    closed = true;
    response.writeHead(204).end();
    return;
  }
  response.writeHead(409, { 'Content-Type': 'application/problem+json' });
  response.end(JSON.stringify({
    type: conflict,
    title: 'Report already closed',
    status: 409,
    detail: 'Report 27 was already closed.',
    instance: '/problems/occurrences/2',
  }));
});

server.listen(0, '127.0.0.1');
await once(server, 'listening');
try {
  const endpoint = 'http://127.0.0.1:' + server.address().port + '/reports/27/close';
  const first = await fetch(endpoint, { method: 'POST' });
  assert.equal(first.status, 204);
  assert.equal(await first.text(), '');

  const second = await fetch(endpoint, { method: 'POST' });
  assert.equal(second.status, 409);
  assert.equal(second.headers.get('content-type'), 'application/problem+json');
  const problem = await second.json();
  assert.equal(problem.status, second.status);
  assert.equal(problem.type, conflict);
  assert.equal(problem.instance, '/problems/occurrences/2');
  console.log(JSON.stringify({ node: process.version, first: first.status, second: second.status, problem }, null, 2));
} finally {
  server.close();
}

What a client observes at each layer

The printed output records first as 204 and second as 409. The second body carries a status member matching the actual HTTP status. A client can read the network status even when an intermediary produces its own reply. For example, a gateway can answer with a generic page before reaching the application. Check the response content type before trying to parse a problem document, and retain a general handling path for other replies. Once the client has the application problem, it can recognize the closed-report type and refresh the displayed state. Matching the type instead of depending on a particular detail sentence allows editors to revise messages and translators to choose natural wording.

What to communicate and what to keep internal

An occurrence identifier can help a support desk find a corresponding record. That does not require putting internal paths, queries or personal information in the response. The instance in the example identifies the problem occurrence without making that URL an additional public endpoint. In production, support can correlate events through authorized logging while the client receives a narrow explanation about the operation it tried. Consider whether a suggested client action actually makes progress. With a closed report, refreshing the view may be appropriate. Repeating the same action will encounter the same state. Treating a state conflict like a temporary connection failure can make a client retry an operation that cannot succeed.

Using the historical design today

RFC 9457 superseded RFC 7807 in July 2023. The year attached to this article refers to the introduction of the earlier specification. Use the current document when setting a new API contract, especially when defining problem types and checking edge cases. For an existing client, begin migration with a test that inspects the actual HTTP status and content type separately. Add a second problem that shares status 409 and verify the client still selects the appropriate action for each type. Finally test a generic gateway failure and confirm it reaches the fallback path. Keeping success and problem responses in the same exercise makes it easy to see where the client decides which shape to parse and which fields it uses to resolve an error.

466 reads ✎ Write a comment

Reader discussion

Discussion prompt

Which data would you verify before applying this development in your field?

No comments yet. Be the first to contribute.

Write a comment

Comments are real visitor contributions. Repeated, link-heavy or abusive submissions may be limited.