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",
}