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 Author

Nice catch! Thanks, I have updated