Skip to content

HTTP handler functions in Go

In Go's net/http package, an HTTP handler function is a function responsible for processing an incoming HTTP request and generating a response.^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md]

Function Signature

To be compatible with the HTTP server, a handler function must adhere to a specific signature required by the http.HandleFunc registration method^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md]. This signature accepts two arguments^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md]:

  • http.ResponseWriter: An interface used to construct the HTTP response.
  • *http.Request: A pointer to a struct containing the request details, such as the body, headers, and URL.

The standard definition looks like this^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md]:

func MyHandler(w http.ResponseWriter, r *http.Request) {
    // Handler logic
}

Registration

Handler functions are registered with the default HTTP router (DefaultServeMux) using the http.HandleFunc function^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md]. This function takes two arguments: a route pattern (e.g., / or /update) and the handler function to execute when that path is matched^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md].

Writing Responses

The http.ResponseWriter interface provides methods for sending data back to the client^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md]. The Write() method accepts a slice of bytes ([]byte) and writes it to the response body^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md].

To send a string, it must be converted to bytes, for example: w.Write([]byte("Hello!"))^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md].

Manipulating Headers

Reading Request Headers

The incoming request headers can be accessed via r.Header^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md]. This acts as a map (dictionary) where iterating over it yields the header key and its corresponding value(s)^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md].

for header, value := range r.Header {
    fmt.Printf("Key: %v \t Value: %v \n", header, value)
}

Setting Response Headers

Headers can be added to the response using the Header().Add() method on the writer^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md].

w.Header().Add("TestHeader", "TestValue")

Handling Methods and Status Codes

Handlers can check the r.Method property to determine the HTTP method (e.g., GET, POST) and act accordingly^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md]. If a method is not supported or a request is invalid, the server should return an appropriate HTTP status code using w.WriteHeader()^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md].

For example, returning a 405 Method Not Allowed status^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md]:

if r.Method != "POST" {
    w.WriteHeader(405)
    fmt.Fprintf(w, "Method not Supported!")
}
  • [[Go]]
  • [[HTTP]]
  • [[API]]

Sources

^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md]