Module 07 Β· Building a REST API β Lesson 3 of 3 Β· ~9 min
Decoding & validating requests
Reading the request body
A POST /bookings carries a JSON body β the walker id, the date, how long. On the server, that body is r.Body, a stream of bytes. You decode it into a Go struct with encoding/json:
type createBookingReq struct {
WalkerID string `json:"walker_id"`
Minutes int `json:"minutes"`
}The json:"walker_id" struct tag tells the decoder which JSON key fills which field. (Tags live inside Go backticks, so you'll only ever see them in read-only snippets like this one β never type one into an exercise.)