liveBook · Manning

HTTP GET

As explained in Learn Go with Pocket-Sized Projects

Get is a url.Values method that returns the first value associated with a specified query parameter key.

HTTP GET accesses a resource through an endpoint, where the endpoint combines an HTTP method with a path. GET commonly retrieves a resource or displays its state, while POST creates a resource or requests processing. section 8.2

A resource can be identified directly by a path, such as /games/{game_id}. A GET request to that route retrieves one game's status; the identifier comes from the path rather than the request body. section 8.2.2

GET-based handlers commonly work with:

  • Path parameters, which target a specific resource such as /games/{gameID}. section 8.2.2
  • Query parameters, which filter or qualify a request and follow the URL after a question mark. section 8.2.2
  • Idempotent behavior: repeated GET calls should return the same response and leave resources unchanged. section 8.2

Reading a query parameter in Go

For a URL such as http://localhost:8080/game?lang=en, retrieve the language with lang := r.URL.Query().Get(api.Lang). URL.Query returns url.Values, whose Get(key string) string method returns the first value associated with the key. A handler can then use the value when creating the game. section 8.6.5

...
    r.HandleFunc(http.MethodPost+" "+api.NewGameRoute, 
        newgame.Handle) #1
    r.HandleFunc(http.MethodGet+" "+api.GetStatusRoute, 
        getstatus.Handle) #2
...

Routes a GET request to the game-status handler with http.MethodGet and the /games/{id} path. section 8.2.2

Listing 8.15 getstatus/handler_test.go: Testing handle
func TestHandle(t *testing.T) {
    req, err := http.NewRequest(http.MethodGet,
        "/games/", nil) #1
    require.NoError(t, err)

    // add path parameter
    req.SetPathValue(api.GameID, "123456")


    recorder := httptest.NewRecorder() #2

    Handle(recorder, req)

    assert.Equal(t, http.StatusOK, recorder.Code)
    assert.JSONEq(t, `{"id":"123456","attempts_left":0,"guesses":[],
        "word_length":0,"status":""}`, recorder.Body.String())
}

Tests a GET request, supplies a path value, invokes the handler, and checks the HTTP status and JSON response. section 8.2.2

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Explains methods, routes, path and query parameters, idempotence, handlers, and tests, but not deployment or containerization.
Examples
Uses game-status routes, language query parameters, Go handlers, request tests, and JSON responses.
What it assumes
Assumes familiarity with HTTP servers and Go handlers while introducing endpoint structure and parameter-decoding techniques.
What you can do after
Apply it via Go's net/http APIs to register GET routes, read parameters, invoke handlers, and test responses.
How it connects
Connects HTTP endpoints with REST APIs, path parameters, query parameters, handlers, POST, and idempotence.

Also explained in

Compare how 14 books explain HTTP GET →

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.