Skip to content

Golang: Handling Json Post Request Snippet

Create a new Decoder from json package and pass the request body to it! Simple, simple, simple.

Try this snippet and enjoy:

type User struct {
	Name string `json:"name"`

func userHandler(rw http.ResponseWriter, req *http.Request) {
	var user User

        // A Decoder reads and decodes JSON values from an input stream.
	decoder := json.NewDecoder(req.Body)

	err := decoder.Decode(&user)
	if err != nil {


Leave a Reply

Your email address will not be published. Required fields are marked *