liveBook · Manning

HTTP method

As explained in Go in Practice, Second Edition

An HTTP method is a standardized operation token in an HTTP request that specifies the intended action on a resource.

An HTTP method is a standardized operation token in a request. Together with the path, it lets a router select the handler and expected response for a resource. section 8.1.1

Use methods to assign different operations to the same endpoint:

  • GET commonly retrieves data, while POST commonly creates it. section 8.1.1
  • PUT, PATCH, DELETE, and OPTIONS are available for APIs that require those operations, but client support may vary. section 8.1.1
  • Avoid treating CRUD as a mandatory architecture; choose the endpoint design according to application goals. section 8.1.1

One comments endpoint, two handlers

Go's standard router can register method-specific patterns such as GET /comments and POST /comments. A GET handler can format stored comments, while a POST handler reads the request body, appends a comment, and records its timestamp. Test creation with curl -X POST http://localhost:8004/comments -d "I'm new here". section 8.1.1

Listing 8.1 Using built-in method routing
package main

import (
    "net/http"
)

func getComments(w http.ResponseWriter, 
r *http.Request) {                        #1
    w.Write([]byte("here you'll get the comments"))
}

func postComments(w http.ResponseWriter, 
r *http.Request) {                      #2
    w.Write([]byte("thank you for posting a comment"))
}

func main() {
    http.HandleFunc("GET /comments", getComments)     #3
    http.HandleFunc("POST /comments", postComments)              #4
    if err := http.ListenAndServe(":8000", nil); err != nil {
        panic(err)
    }
}

Shows a complete runnable Go server using built-in method routing for separate GET and POST handlers. section 8.1.1

When routing needs extra request-based decisions, a wrapper can inspect r.Method manually and dispatch further, such as separating comment creation from updating based on decoded JSON. section 8.1.1

How Go in Practice, Second Edition covers it

How deep it goes
Explains built-in and manual method routing with runnable handlers, but stops short of comprehensive HTTP method semantics.
Examples
Demonstrates comments retrieval and creation, curl submission, method-specific patterns, and manual JSON-based upsert dispatch.
What it assumes
Assumes basic Go HTTP server and handler knowledge while introducing routing decisions from request paths and methods.
What you can do after
Build Go HTTP routes that dispatch GET and POST requests, or inspect methods manually for additional application logic.
How it connects
Connects HTTP methods to routing, REST, CRUD, request bodies, JSON decoding, and form processing.

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 Go in Practice, Second Edition. The section links point to the parts of the book it draws on.