Skip to main content

Say What You Mean

A REST API talks through two vocabularies. The method says what the client wants: GET read, POST create, PUT replace, PATCH change a part, DELETE remove. The status code says how it went: 2xx success, 4xx the client's mistake, 5xx the server's.

A detail worth knowing: PUT is idempotent — sending it twice ends the same as once. POST is not: two POSTs create two resources.


The Right Method

The method patterns in the mux route each verb to its own handler. Creation answers 201 Created and points at the new resource with a Location header; deletion answers 204 No Content — success with nothing to say.

GET, POST and DELETE on one resource

Three verbs, three answers: the list comes back with 200, the creation with 201 and a Location, the deletion with 204 and an empty body.

Prompt
Write a Go program with a mux handling GET /notes (200 with a list line), POST /notes (201 with a Location header) and DELETE /notes/{id} (204, empty body). Serve with httptest, send the three requests and print each method, status code and any Location header or body.
Output: Go program and captured output

The Right Status Code

Errors deserve precision too: a missing resource is 404 Not Found (with a JSON error body so machines can read it), and a wrong verb on a known path is 405 Method Not Allowed — which Go's mux answers for you when the path matches but the method does not.

200, 404 and 405

The existing note returns 200 with data, the missing one 404 with a JSON error, and the DELETE on a GET-only route is refused with 405 by the router itself.

Prompt
Write a Go program with a GET /notes/{id} handler that returns 200 with JSON for an existing note and 404 with a JSON error body for a missing one. Serve with httptest, request an existing note, a missing note, and send a DELETE to the same path to show the automatic 405. Print all three results.
Output: Go program and captured output