I'm new to Go so I'm trying to build a simple CRUD API using Chi. In the GetById handler I'm using chi.URLParam() method to retrieve data from the URL, but this method is returning an empty string "". Check the code:
func GetBookByIDHandler(w http.ResponseWriter, r *http.Request) {
db, err := repository.OpenConnection()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Printf("Error while opening connection: %v", err)
return
}
defer db.Close()
repo := repository.NewBookSQLRepository(db)
useCase := usecases.NewFindBookByIDUseCase(repo)
idParam := chi.URLParam(r, "id")
//idParams = ""
var input usecases.FindBookByIDInput
input.ID = idParam
output, err := useCase.FindBookByID(input)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Printf("Error while getting book by id: %v", err)
return
}
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(output)
}
My router is built as follows:
r := chi.NewRouter()
r.Get("/books", handlers.GetAllBooksHandler)
r.Get("/books/{id}", handlers.GetBookByIDHandler)
r.Post("/books", handlers.CreateBookHandler)
I'm using Insomnia for the HTTP requests, in the following URL: http://localhost:8080/books/10
I already tried to make the request passing query params as "?id=10" but it did not work.
I'm having the exact same issue and there's no answers for me, but I do I have a workaround which extracts the id in the conventional way, below is the working version of your code: