DEV Community

Discussion on: Build a REST API with Go - For Beginners

Collapse
 
karimelngr profile image
Karim ElNaggar

Thanks for the tutorial, I think there is a small bug here

w.WriteHeader(http.StatusOK)
w.Header().Add("Content-Type", "application/json")
Enter fullscreen mode Exit fullscreen mode

should be

w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
Enter fullscreen mode Exit fullscreen mode

otherwise the content type header does not get written
Another thing, I think you switched two lines

router.HandleFunc("/books/{id}", handlers.GetBook).Methods(http.MethodGet)
// and
router.HandleFunc("/books", handlers.AddBook).Methods(http.MethodPost)
Enter fullscreen mode Exit fullscreen mode

between the two sections Add a new Book and Get a book by id

Thanks again for the great tutorial

Collapse
 
karanpratapsingh profile image
Karan Pratap Singh

Nice catch! Thanks, I have updated

Collapse
 
sethjoe12 profile image
karl joseph

hai sir good day can you help me with this thing .it was a code structure sir.

i edited all your codes and it comes with this error

"..\pkg\test\Additem.go:24:19: cannot use item (type models.Item) as type []byte in argument to json.Unmarshal"