Why r.body is empty

I have a handler in my main function

router.HandleFunc("/books", addBook).Methods("POST")

and an addBook function which must add new books into my Book slice

func addBook(w http.ResponseWriter, r *http.Request) {
  var book Book
  fmt.Println(r.Body)
  json.NewDecoder(r.Body).Decode(&book)

  books = append(books, book)

  json.NewEncoder(w).Encode(books)
}

but unfortunately, when I made a post request in postman with the row code inside the body,
the addBook function adds empty strings and zero value into my Book slice
here is my book type

type Book struct {
     ID      int      `json:"id"`
     Title	string   `json:"title"`
     Author  string   `json:"author"`
     Year    string   `json:"year"`
}

my query in postman

{
	"id": 7,
	"title": "Mai",
	"author": "Selpak",
	"year": "2017",
}

SOLVED, My bad, forgot to mark the query as a json, inside postman) sorry

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.