Different Methods on One Web Route

A resource path can support several HTTP methods with distinct handler behavior, even when they operate on the same record.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace a read and an update
  2. Decide how other methods are reported
  3. Check the same path with both supported roles
  4. Assert persistent effects and redirects in separate cases

Trace a read and an update

A route may return the current record on GET and process a supplied replacement on PUT. The path selects the record and the method selects the type of action. HTTP methods describe those semantics.

Decide how other methods are reported

An unsupported method outcome differs from no matching resource or missing input. Check it independently when testing the endpoint. See reading response status.[1][2]

Check the same path with both supported roles

A route at /notes/new can render a form on GET and validate a creation request on POST. Matching the path alone does not determine which action should happen. An unsupported method generally has its own status and may advertise allowed methods. See HTTP method intent.

GET /notes/new → display form
POST /notes/new with valid input → attempt creation
An unsupported method → method error under the documented contract

Assert persistent effects and redirects in separate cases

Reading the form should leave record count unchanged under its read semantics. Successful submission should create the expected item and return the documented result. Test invalid submissions while confirming no unwanted item was stored.

References

  1. IETF, RFC 9110: HTTP Semantics ↩
  2. WHATWG, HTML Living Standard: Forms ↩