net/go.book
All Parts Marketing Resources PDF

Building RESTful APIs with net/http

Go's standard library is unusually capable for building production APIs without a framework. This chapter puts everything from the previous two chapters together — clean routing, query parameters, JSON serialization — into a complete, working CRUD API using only net/http. No third-party router, no framework: just the tools that ship with Go.


The Resource: An In-Memory Task Store

We'll build a small task-tracking API. Tasks live in memory, guarded by a mutex, so concurrent requests don't race:

package main

import (
	"encoding/json"
	"errors"
	"log"
	"net/http"
	"strconv"
	"sync"
	"time"
)

type Task struct {
	ID   int    `json:"id"`
	Text string `json:"text"`
	Done bool   `json:"done"`
}

type Store struct {
	mu     sync.Mutex
	nextID int
	tasks  map[int]Task
}

// errNotFound is a sentinel: handlers test it with errors.Is instead of
// guessing that any error from the store means "404".
var errNotFound = errors.New("task not found")

func NewStore() *Store {
	return &Store{nextID: 1, tasks: make(map[int]Task)}
}

func (s *Store) Create(text string) Task {
	s.mu.Lock()
	defer s.mu.Unlock()
	t := Task{ID: s.nextID, Text: text}
	s.tasks[t.ID] = t
	s.nextID++
	return t
}

func (s *Store) List() []Task {
	s.mu.Lock()
	defer s.mu.Unlock()
	out := make([]Task, 0, len(s.tasks))
	for _, t := range s.tasks {
		out = append(out, t)
	}
	return out
}

func (s *Store) Get(id int) (Task, error) {
	s.mu.Lock()
	defer s.mu.Unlock()
	t, ok := s.tasks[id]
	if !ok {
		return Task{}, errNotFound
	}
	return t, nil
}

func (s *Store) Update(id int, done bool) (Task, error) {
	s.mu.Lock()
	defer s.mu.Unlock()
	t, ok := s.tasks[id]
	if !ok {
		return Task{}, errNotFound
	}
	t.Done = done
	s.tasks[id] = t
	return t, nil
}

func (s *Store) Delete(id int) error {
	s.mu.Lock()
	defer s.mu.Unlock()
	if _, ok := s.tasks[id]; !ok {
		return errNotFound
	}
	delete(s.tasks, id)
	return nil
}

Wiring Up the Handlers

Using the Go 1.22+ http.ServeMux method-and-wildcard patterns from the previous chapter, each CRUD operation gets its own method-scoped route:

type API struct {
	store *Store
}

func writeJSON(w http.ResponseWriter, status int, v any) {
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(status)
	if err := json.NewEncoder(w).Encode(v); err != nil {
		// The status line is already sent, so this can only be logged.
		log.Printf("writeJSON: %v", err)
	}
}

// fail writes a JSON error body. The message is for humans; clients should
// branch on the status code, not on this string.
func fail(w http.ResponseWriter, status int, msg string) {
	writeJSON(w, status, map[string]string{"error": msg})
}

func (a *API) createTask(w http.ResponseWriter, r *http.Request) {
	// Never decode an unbounded body: one client could otherwise stream
	// gigabytes into memory. 1 MiB is plenty for this API.
	r.Body = http.MaxBytesReader(w, r.Body, 1<<20)

	var body struct {
		Text string `json:"text"`
	}
	dec := json.NewDecoder(r.Body)
	dec.DisallowUnknownFields()
	if err := dec.Decode(&body); err != nil || body.Text == "" {
		fail(w, http.StatusBadRequest, "text is required")
		return
	}
	task := a.store.Create(body.Text)
	writeJSON(w, http.StatusCreated, task)
}

func (a *API) listTasks(w http.ResponseWriter, r *http.Request) {
	writeJSON(w, http.StatusOK, a.store.List())
}

func (a *API) getTask(w http.ResponseWriter, r *http.Request) {
	id, err := strconv.Atoi(r.PathValue("id"))
	if err != nil {
		fail(w, http.StatusBadRequest, "invalid id")
		return
	}
	task, err := a.store.Get(id)
	switch {
	case errors.Is(err, errNotFound):
		fail(w, http.StatusNotFound, "task not found")
	case err != nil:
		fail(w, http.StatusInternalServerError, "internal error")
	default:
		writeJSON(w, http.StatusOK, task)
	}
}

func (a *API) updateTask(w http.ResponseWriter, r *http.Request) {
	id, err := strconv.Atoi(r.PathValue("id"))
	if err != nil {
		fail(w, http.StatusBadRequest, "invalid id")
		return
	}
	r.Body = http.MaxBytesReader(w, r.Body, 1<<20)
	var body struct {
		Done bool `json:"done"`
	}
	if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
		fail(w, http.StatusBadRequest, "invalid body")
		return
	}
	task, err := a.store.Update(id, body.Done)
	switch {
	case errors.Is(err, errNotFound):
		fail(w, http.StatusNotFound, "task not found")
	case err != nil:
		fail(w, http.StatusInternalServerError, "internal error")
	default:
		writeJSON(w, http.StatusOK, task)
	}
}

func (a *API) deleteTask(w http.ResponseWriter, r *http.Request) {
	id, err := strconv.Atoi(r.PathValue("id"))
	if err != nil {
		fail(w, http.StatusBadRequest, "invalid id")
		return
	}
	switch err := a.store.Delete(id); {
	case errors.Is(err, errNotFound):
		fail(w, http.StatusNotFound, "task not found")
	case err != nil:
		fail(w, http.StatusInternalServerError, "internal error")
	default:
		w.WriteHeader(http.StatusNoContent)
	}
}

func main() {
	api := &API{store: NewStore()}
	mux := http.NewServeMux()
	mux.HandleFunc("POST /tasks", api.createTask)
	mux.HandleFunc("GET /tasks", api.listTasks)
	mux.HandleFunc("GET /tasks/{id}", api.getTask)
	mux.HandleFunc("PATCH /tasks/{id}", api.updateTask)
	mux.HandleFunc("DELETE /tasks/{id}", api.deleteTask)

	srv := &http.Server{
		Addr:              ":8080",
		Handler:           mux,
		ReadHeaderTimeout: 5 * time.Second,
		ReadTimeout:       15 * time.Second,
		WriteTimeout:      15 * time.Second,
		IdleTimeout:       60 * time.Second,
	}
	if err := srv.ListenAndServe(); err != nil {
		log.Fatal(err)
	}
}

Status Codes Are Part of the API

The handlers above draw a line you will reuse in every API you write: a malformed request (bad path parameter, unreadable JSON) is 400 Bad Request; a well-formed request for something that does not exist is 404 Not Found; an unexpected failure inside your code is 500 Internal Server Error; a successful create is 201 Created; a successful delete with nothing left to say is 204 No Content.

That last one matters for a subtle reason: DELETE and PUT are idempotent by design. Repeating the same request should leave the server in the same state, which is why a second DELETE /tasks/1 returning 404 is honest and useful, while a POST /tasks that silently creates a duplicate is not. If you want a delete that always succeeds (a "make sure it is gone" operation), return 204 even when the row was already missing, and document that choice. What you should not do is what the first draft of these handlers did: collapse every store error into 404, which turns a locked mutex or a future database outage into a lie about the client's URL.

The sentinel error is what makes the distinction possible. errors.Is(err, errNotFound) asks "is this specifically the not-found error, possibly wrapped?" rather than "is there any error at all?", so unexpected errors still surface as 500 instead of disappearing into a misleading 404.

Request:

POST /tasks HTTP/1.1
Content-Type: application/json

{"text": "Write chapter 4"}

Response:

HTTP/1.1 201 Created
Content-Type: application/json

{"id":1,"text":"Write chapter 4","done":false}

Middleware: Wrapping Handlers

Cross-cutting concerns — logging, recovery, request timing — belong in middleware, not scattered across every handler. In net/http, middleware is just a function that wraps a http.Handler and returns another one:

func loggingMiddleware(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		start := time.Now()
		next.ServeHTTP(w, r)
		log.Printf("%s %s %s", r.Method, r.URL.Path, time.Since(start))
	})
}

func recoverMiddleware(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		defer func() {
			if err := recover(); err != nil {
				log.Printf("panic recovered: %v", err)
				http.Error(w, "internal server error",
					http.StatusInternalServerError)
			}
		}()
		next.ServeHTTP(w, r)
	})
}

Chain them around the mux once, at the top level:

handler := loggingMiddleware(recoverMiddleware(mux))
// then use handler as the http.Server's Handler field

Because each middleware only depends on http.Handler, you can stack as many as you need, in any order, without touching the handlers themselves.


Graceful Shutdown

Production services should stop accepting new connections and let in-flight requests finish before exiting:

// Needs "context", "os/signal" and "syscall" added to the imports.
srv := &http.Server{
	Addr:              ":8080",
	Handler:           handler,
	ReadHeaderTimeout: 5 * time.Second,
	ReadTimeout:       15 * time.Second,
	WriteTimeout:      15 * time.Second,
	IdleTimeout:       60 * time.Second,
}

go func() {
	if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
		log.Fatal(err)
	}
}()

ctx, stop := signal.NotifyContext(
	context.Background(), os.Interrupt, syscall.SIGTERM,
)
defer stop()
<-ctx.Done()

shutdownCtx, cancel := context.WithTimeout(
	context.Background(), 10*time.Second,
)
defer cancel()
if err := srv.Shutdown(shutdownCtx); err != nil {
	log.Printf("graceful shutdown: %v", err)
}

Shutdown stops accepting new connections and waits for in-flight requests, which is exactly what you want on a deploy: a client mid-upload gets to finish instead of seeing the connection vanish. The ten-second context is the upper bound: past it, Shutdown returns an error and you decide whether to exit anyway. Ignoring that error, as the earlier draft did, means a stuck request can hang your process forever with no record of why.

Context: The Request's Own Lifetime

Every *http.Request carries r.Context(), which is canceled the moment the client disconnects or the server shuts down. Passing it into anything slow (a database call, an outbound HTTP request, a queue publish) is what makes cancellation actually propagate:

upstream, err := http.NewRequestWithContext(
	r.Context(), http.MethodGet, "https://api.example.com/rates", nil,
)
if err != nil {
	fail(w, http.StatusInternalServerError, "bad upstream request")
	return
}
resp, err := http.DefaultClient.Do(upstream)
if err != nil {
	// Includes context.Canceled when the client hung up first.
	fail(w, http.StatusBadGateway, "upstream unavailable")
	return
}
defer resp.Body.Close()

The same rule applies to the database: db.QueryRowContext(r.Context(), ...) instead of db.QueryRow(...). The store in this chapter is in-memory, so it has nothing to cancel, which is why the original code can get away with ignoring the context. The moment a handler talks to Postgres, Redis, or another service, using context.Background() instead of r.Context() means the work keeps running after the user gave up, and under load that is how a small timeout becomes a pile of orphaned queries.

Why Stick to the Standard Library

Frameworks like Gin and Fiber (covered next) add convenience — route grouping, built-in binding, richer middleware ecosystems — but net/http alone is enough for most services, has zero dependencies to audit or upgrade, and its APIs are stable across Go versions for the lifetime of your project. Many teams start here and only reach for a framework once the routing or middleware boilerplate becomes genuinely painful.

Frequently Asked Questions

Why does Store use a plain sync.Mutex instead of a sync.RWMutex for a map that's read far more often than it's written? It would be a fair optimization for a read-heavy task list, but the extra complexity isn't worth it in a teaching example — a single Mutex is easier to reason about and impossible to get wrong. Once you've profiled a real service and found the lock genuinely contended by concurrent reads, swapping in an RWMutex is a mechanical, low-risk change.

Why call dec.DisallowUnknownFields() in createTask but not in updateTask? createTask is the stricter of the two on purpose — it's the entry point where a typo in a client's JSON (like "txt" instead of "text") should fail loudly rather than silently create a task with an empty description. updateTask only touches the done flag, so the risk of a silently ignored field is lower, but you could just as reasonably tighten it the same way.

My handler returns a 404 for GET /tasks/{id} even though the task exists — what's usually wrong? Nine times out of ten it's a mismatch between the numeric ID in the URL and the one stored in the map, often because strconv.Atoi(r.PathValue("id")) succeeded but parsed a value that was never actually created. Add a quick log of the parsed id right after the Atoi call and compare it against store.List() — the bug is almost always upstream of the store itself.

Do I really need both a panic-recovery middleware and graceful shutdown, since net/http already recovers per-request panics? Yes, and they solve different problems. Per-request recovery (built into net/http and reinforced by recoverMiddleware) stops one bad request from taking down the whole process; graceful shutdown makes sure that when you do deliberately stop the process — a deploy, a SIGTERM from your orchestrator — in-flight requests get to finish instead of being cut off mid-response.

How does the routing here compare to what Gin gives me? The method-and-wildcard patterns on http.ServeMux ("POST /tasks", "GET /tasks/{id}") already get you most of what a router library provides — method matching and path parameters — which is exactly why this chapter could build a full CRUD API without a framework at all. What Gin adds on top is convenience: route grouping, built-in JSON binding and validation, and a larger middleware ecosystem, not capabilities that are fundamentally missing from the standard library.

Next, we'll see how Gin trims that boilerplate down while keeping the same REST principles.

Key Takeaways

  • net/http plus ServeMux method-and-wildcard patterns is enough for a real CRUD API; a framework buys convenience, not capability.
  • Distinguish 400, 404, 500, 201 and 204 deliberately, and use a sentinel error with errors.Is so unexpected failures do not masquerade as missing resources.
  • Always bound what a client can send: http.MaxBytesReader before decoding, and a decoder with DisallowUnknownFields when strictness helps.
  • A production http.Server sets ReadHeaderTimeout, ReadTimeout, WriteTimeout and IdleTimeout; a bare ListenAndServe sets none.
  • Propagate r.Context() into every slow call so a disconnected client stops the work it started.
  • Middleware is just func(http.Handler) http.Handler, so it composes in any order without touching your handlers.
  • Graceful shutdown on SIGTERM is what makes deploys invisible: stop accepting, let in-flight requests finish, then exit.
Discuss this chapter Star the book
Comment on GitHub
Rate