liveBook · Manning

HTTP method

As explained in Learn Go with Pocket-Sized Projects

An HTTP method identifies the intended operation of an HTTP request, such as retrieving or submitting a resource.

A game service needs to create games, accept guesses, and report game status. Each HTTP endpoint pairs a path with a method: the path identifies a resource, while the method identifies the requested action. Use `/games` for the collection and `/games/{game_id}` when addressing one game. section 8.2

Choose the method to match the action:

GET, PUT, and DELETE should be idempotent: repeating a request should leave the resource in the same state as making it once. Use POST instead for a non-idempotent endpoint, and document any API behavior that is not idempotent. section 8.2

Create a game with POST /games

Creating a game does not require input, but the response needs an ID for later guess and status requests. Choose `POST /games` and return that ID. If a resource ID is already known, PUT can instead be suitable for creation—for example, a book at `/books/9781633438804`. section 8.2.1 section 8.2.1

In Go, a handler can reject `req.Method != http.MethodPost` with `http.StatusMethodNotAllowed`; a method-and-path router registration is another option. A plain `curl -v localhost:8080/games` sends GET and receives 405, while `curl -v -X POST localhost:8080/games` reaches the creation handler. Set `http.StatusCreated` before writing the response body: writing first produces an unwanted default 200 response. Test the POST handler with `http.NewRequest` and `httptest.NewRecorder`, checking for 201 and the expected JSON response. section 8.2.1

Listing 8.10 internal/handlers/router.go: Setting the verb and path
package handlers

import (
    "net/http" 

    "learngo-pockets/httpgordle/internal/api"
    "learngo-pockets/httpgordle/internal/handlers/newgame"
)

// NewRouter returns a router that listens for requests
// to the following endpoints:
//   - Create a new game;
//
// The provided router is ready to serve.
func NewRouter() *http.ServeMux { 
    r := http.NewServeMux()
    r.HandleFunc(http.MethodPost+" "+api.NewGameRoute, #1
        newgame.Handle)  #1


    return r
}

The router registers the HTTP method and resource path together, making `POST /games` the route to the creation handler. section 8.2.1

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Defines four methods and idempotency, then works through POST routing, rejection of other methods, response status, and a handler test.
Examples
Uses `POST /games`, a PUT request to an ISBN-based book path, curl requests, and a Go handler test.
What it assumes
Builds on an existing Go game but introduces endpoint and HTTP method basics before implementing its web handler.
What you can do after
Build it from scratch: route `POST /games`, reject an unsupported method with 405, and test a 201 JSON response.
How it connects
Connects HTTP endpoints and resource paths to idempotency, routing, status codes, and handler tests.

Also explained in

Compare how 10 books explain HTTP method →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Go with Pocket-Sized Projects. The section links point to the parts of the book it draws on.