liveBook · Manning

Payload

As explained in Learn Go with Pocket-Sized Projects

A request body is the content-bearing part of an HTTP request that carries data submitted to a server.

A payload is the content-bearing part of an HTTP request: the request body carries data submitted to a server. It complements path parameters, query parameters, and headers as a way to provide input to an API. section 8.2.3 section 8.2.2

A request-body payload is shaped and processed through a small API contract:

  • Choose a representation, such as a JSON object containing only the fields the operation needs. section 8.2.3
  • Define a Go structure for the payload and use JSON tags to map fields to JSON names. section 8.2.3
  • Decode request.Body with json.NewDecoder, then return HTTP 400 when decoding fails. section 8.2.3

Submitting and decoding a guess

A Gordle guess uses PUT /games/{game_id} with the payload {"guess":"hello"}. The handler models it as GuessRequest and decodes it with json.NewDecoder(request.Body).Decode(&r). Invalid JSON produces a bad-request response. section 8.2.3

Listing 8.17 guess/handler.go: How to read a request body
// Read the request, containing the guess, from the body of the input.
r := api.GuessRequest{} #1
err := json.NewDecoder(request.Body).Decode(&r) #2
if err != nil {
    http.Error(writer, err.Error(), 
        http.StatusBadRequest) #3
    return
}

Inspect the complete Go handler fragment that reads a request body, decodes JSON, reports malformed input, and stops processing. section 8.2.3

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Explains payload shape, Go decoding, JSON tags, and malformed-input handling, but does not detail validation beyond decoding.
Examples
Uses a Gordle guess payload, GuessRequest, PUT /games/{game_id}, and json.NewDecoder(request.Body).Decode.
What it assumes
Assumes familiarity with HTTP endpoints and Go JSON decoding while distinguishing request bodies from path and query parameters.
What you can do after
Build request handlers that decode JSON payloads and return HTTP 400 for malformed request bodies.
How it connects
Connects request bodies with HTTP methods, JSON, REST APIs, path parameters, query parameters, and HTTP handlers.

Also explained in

Compare how 8 books explain payload →

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.