Golang gorilla mux REST api 在使用 PUT 和 DELETE 方法时出现 405 错误
Golang gorilla mux REST api is giving 405 error on using PUT and DELETE methods
Golang gorilla mux REST api 在使用 PUT 和 DELETE 方法时出现 405 错误。但是 GET 和 POST 方法工作正常。
我使用 postman 测试我的 REST api 服务器,GET 和 POST 请求工作正常但 PUT 和 DELETE 方法不工作并给出 error 405
...
...
func updateBook(w http.ResponseWriter, r *http.Request){
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r)
for index, item := range books {
if item.ID == params["id"]{
books = append(books[:index], books[index+1:]...)
var book Book
_ = json.NewDecoder(r.Body).Decode(&book)
book.ID = params["id"]
books = append(books, book)
json.NewEncoder(w).Encode(books)
return
}
}
}
func deleteBook(w http.ResponseWriter, r *http.Request){
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r)
for index, item := range books {
if item.ID == params["id"]{
books = append(books[:index], books[index+1:]...)
break
}
}
json.NewEncoder(w).Encode(books)
}
func main() {
// INIT router
r := mux.NewRouter()
// Mock data
books = append(books, Book{
ID: "1",
Isbn: "43432",
Title: "Book first",
Author: &Author{
FirstName: "Vishal",
LastName: "Sharma",
},
})
// Route handlers / Endpoints
r.HandleFunc("/api/books", getbooks).Methods("GET")
r.HandleFunc("/api/books/{id}", getBook).Methods("GET")
r.HandleFunc("/api/books", createBook).Methods("POST")
r.HandleFunc("api/books/{id}", updateBook).Methods("PUT")
r.HandleFunc("api/books/{id}", deleteBook).Methods("DELETE")
log.Fatal(http.ListenAndServe(":3000", r))
}
正如评论中指出的那样,这是一个错字。
r.HandleFunc("api/books/{1}", deleteBook).Methods("DELETE")
我在 api/books/{1}
之前缺少一个 / 正斜杠
正确的语法是
r.HandleFunc("/api/books/{1}", deleteBook).Methods("DELETE")
Golang gorilla mux REST api 在使用 PUT 和 DELETE 方法时出现 405 错误。但是 GET 和 POST 方法工作正常。
我使用 postman 测试我的 REST api 服务器,GET 和 POST 请求工作正常但 PUT 和 DELETE 方法不工作并给出 error 405
...
...
func updateBook(w http.ResponseWriter, r *http.Request){
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r)
for index, item := range books {
if item.ID == params["id"]{
books = append(books[:index], books[index+1:]...)
var book Book
_ = json.NewDecoder(r.Body).Decode(&book)
book.ID = params["id"]
books = append(books, book)
json.NewEncoder(w).Encode(books)
return
}
}
}
func deleteBook(w http.ResponseWriter, r *http.Request){
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r)
for index, item := range books {
if item.ID == params["id"]{
books = append(books[:index], books[index+1:]...)
break
}
}
json.NewEncoder(w).Encode(books)
}
func main() {
// INIT router
r := mux.NewRouter()
// Mock data
books = append(books, Book{
ID: "1",
Isbn: "43432",
Title: "Book first",
Author: &Author{
FirstName: "Vishal",
LastName: "Sharma",
},
})
// Route handlers / Endpoints
r.HandleFunc("/api/books", getbooks).Methods("GET")
r.HandleFunc("/api/books/{id}", getBook).Methods("GET")
r.HandleFunc("/api/books", createBook).Methods("POST")
r.HandleFunc("api/books/{id}", updateBook).Methods("PUT")
r.HandleFunc("api/books/{id}", deleteBook).Methods("DELETE")
log.Fatal(http.ListenAndServe(":3000", r))
}
正如评论中指出的那样,这是一个错字。
r.HandleFunc("api/books/{1}", deleteBook).Methods("DELETE")
我在 api/books/{1}
之前缺少一个 / 正斜杠正确的语法是
r.HandleFunc("/api/books/{1}", deleteBook).Methods("DELETE")