-2

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?

Chikku Jacob
  • 2,114
  • 1
  • 18
  • 33

2 Answers2

1

The method attribute only supports the GET and POST HTTP verbs.

To use PUT or DELETE you would need to make the request with fetch or XMLHttpRequest.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • @ex4 — That looks like a typo in the question title. The body of the question says DELETE instead of POST and all the code shows DELETE instead of POST. – Quentin Jul 26 '21 at 11:49
  • Yes, it was a typo in the title. I have updated it to PUT and Delete – Chikku Jacob Jul 26 '21 at 12:57
-3

I would look at CORS (https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) if your web server is running other host / port than your API.

As suggested in comments, check your browsers JS logs / console. There should be clear error messages if CORS is your problem.

ex4
  • 2,289
  • 1
  • 14
  • 21
  • 1
    CORS has no effect on regular form submissions (to which the Same Origin Policy doesn't apply in the first place) – Quentin Jul 26 '21 at 11:52
  • Yes, but once you start using XMLHttpRequest with any other werb than GET or POST in your script,you can't call it regular form submission anymore. – ex4 Jul 26 '21 at 11:58
  • 2
    If they start using XMLHttpRequest with any verb at all then they won't be performing a regular form submission. They *aren't* using XMLHttpRequest though, they *are* trying to perform a regular form submission (and that's their problem - regular form submissions don't support PUT or DELETE). – Quentin Jul 26 '21 at 12:02