I am trying to write a REST API in Go, all the methods are working fine when I run through postman, but when calling the PUT and Delete methods from the HTML page using JAVA Script function it's not working. Are there any alternatives for the same?
Here are my Go Handlers in main.go file.
func main() {
router := mux.NewRouter()
router.HandleFunc("/", HomePageHandler).Methods("GET")
router.HandleFunc("/demo", CreateDemoDetail).Methods("POST") // Add New row
router.HandleFunc("/demo", GetAllDemoDetails).Methods("GET") // Fetch all details
router.HandleFunc("/demo/{id}", GetDemoDetail).Methods("GET") // Fetch Single row
router.HandleFunc("/demo", UpdateDemoDetails).Methods("PUT") // Update Single row
router.HandleFunc("/demo", DeleteDemo).Methods("DELETE") // Delete Single row
log.Fatal(http.ListenAndServe(":8080", router))
}
Following is the Java Script function which calls the Handlers,
function submitForm(reqtype){
var form = document.getElementById("demoform");
if (reqtype == "Delete"){
form.action = 'http://localhost:8080/demo';
form.method='PUT';
}
if (reqtype == "Update"){
form.action = 'http://localhost:8080/demo';
form.method='DELETE';
}
form.submit();
}
</script>
If i change the handler to router.HandleFunc("/updatedemo", UpdateDemoDetails).Methods("POST") than just /demo and the method to POST it works from java script, but not when its demo/ and the method is PUT/Delete.
Interestingly it works perfectly with Postman when the endpoints are demo/ or get demo and method are POST/GET/PUT/Delete. But not from HTML form with Javascript call.
What is the best approach I should take?