Home > Blockchain >  I am trying to make a simple server with Go language, Why is it not loading the page but keeps showi
I am trying to make a simple server with Go language, Why is it not loading the page but keeps showi

Time:07-08

I am following a tutorial and I have literally crosschecked everything, It seems not to be working still ....................................................................


import (
   "fmt"
   "log"
   "net/http"
)

func formHandler(w http.ResponseWriter, r *http.Request) {
   if err := r.ParseForm(); err != nil {
       fmt.Fprintf(w, "ParseForm() err: %v", err)
       return
   }
   fmt.Fprintf(w, "POST request succesfull")
   name := r.FormValue("name")
   address := r.FormValue("address")
   fmt.Fprintf(w, "Name = %s\n", name)
   fmt.Fprintf(w, "Address = %s\n", address)

}
func helloHandler(w http.ResponseWriter, r *http.Request) {
   if r.URL.Path != "/hello" {
       http.Error(w, "404 not found", http.StatusNotFound)
       return
   }
   if r.Method != "Get" {
       http.Error(w, "method is not supported", http.StatusNotFound)
       return
   }
   fmt.Fprintf(w, "hello!")
}
func main() {
   fileServer := http.FileServer(http.Dir("./static"))
   http.Handle("/", fileServer)
   http.HandleFunc("/form", formHandler)
   http.HandleFunc("/hello", helloHandler)

   fmt.Printf("Starting server at port 8080\n")

   if err := http.ListenAndServe(":8080", nil); err != nil {
       log.Fatal(err)
   }
}

Here is the Error message for all pages

CodePudding user response:

I can see there is one more error in your helloHandler function when you compare the request Method, you should compare it with "GET" and not "Get".

For more info look at: https://pkg.go.dev/net/http#pkg-constants

I ran this code locally it is working fine locally for me. With this Method change I mentioned above. Can you post the exact error you are getting?

CodePudding user response:

I see only one issue in your code, but it is in the /hello handler. You are checking if it is a GET using r.Method != "Get", but HTTP methods are in uppercase, so you should use r.Method != "GET", or even better, use the given constant r.Method != http.MethodGet. That solves the /hello issue for me:

$ curl localhost:8080/hello
hello!

Now, the /form handle issue is not in your code, but happens that you are trying to load form.html instead of calling form, which can work if you have a file called form.html in the static subfolder in your project, like this (which is a very minimalist example):

<!DOCTYPE html>
<html>
  <title>Form</title>
  <body>

    <form method="post" action="/form">
      <label for="name">Name: </label>
      <input type="text" id="name" name="name" />
      <label for="address">Address: </label>
      <input type="text" id="address" name="address" />
      <input type="submit" />
    </form>
  </body>
</html>

That works because you are already handling static files in http.Handle("/", fileServer). I don't know the tutorial you are following, but looks like that was the intention.

Another option to try form directly, without HTML could be using something like curl:

$ curl -d 'name=My Name&address=My Address' localhost:8080/form
POST request succesfullName = My Name
Address = My Address

There are other tools. The HTML one should be fine for training.

  • Related