1

I am trying to create a GoLang http.HandleFunc() that will listen for post requests and parse a json file. The Handler seems to fail as it starts and returns an error:

2017/01/24 13:35:08 listen tcp :8080: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted.

I am assuming that it is throwing a fatal error.

Below is my function:

 http.HandleFunc("/", func(w http.ResponseWriter, request *http.Request) {

        //debug
        fmt.Fprintf(w, "Hello, %q", html.EscapeString(request.URL.Path))

        var m Message

        if request.Body == nil {
            http.Error(w, "Please send a request body", 400)                
            return
        }

        err := json.NewDecoder(request.Body).Decode(&m)
        if err != nil {
            http.Error(w, err.Error(), 400)
            return
        }
 })
 log.Fatal(http.ListenAndServe(":8080", nil))

How can I get this Handler to work and not get the error?

5
  • Your code snippet doesn't make any sense; why is there a defer res.Body.Close() call "in the middle on nowhere"? Commented Jan 24, 2017 at 18:51
  • This error indicates that there is another process or thread listening on the port already. Do you perhaps have another program on your computer serving to port 8080? Commented Jan 24, 2017 at 18:51
  • @TimCooper I'm not sure where it is supposed to go? Any suggestions? Commented Jan 24, 2017 at 19:16
  • @JordanLewis I ran netstat -nc TCP | find "8080" and got nothing back Commented Jan 24, 2017 at 19:17
  • I figured it out. The Handler continues to run as a process on Windows until you either shut down Eclipse, or kill the process. After you do this, you can run the handler without error. How can I get the handler to stop in the code for testing purposes? Commented Jan 24, 2017 at 20:36

1 Answer 1

1

I've remedied several of the problems with your code and made best guesses around missing information. Here is sample working code that will accept a message that has "name" and "text" fields:

package main

import (
  "encoding/json"
  "fmt"
  "html"
  "log"
  "net/http"
)

type Message struct {
  Name string `json:"name"`
  Text string `json:"text"`
}

func main() {
  http.HandleFunc("/", func(w http.ResponseWriter, request *http.Request) {
    fmt.Fprintf(w, "Hello, %q", html.EscapeString(request.URL.Path))

    var m Message

    if request.Body == nil {
      http.Error(w, "Please send a request body", 400)
      return
    }

    err := json.NewDecoder(request.Body).Decode(&m)
    if err != nil {
      http.Error(w, err.Error(), 400)
      return
    }

    log.Println(m)
  })

  log.Fatal(http.ListenAndServe(":8080", nil))
}
Sign up to request clarification or add additional context in comments.

4 Comments

I cannot find much difference. I see that you added the main function and the struct, which I have. I was only showing the Handler fucntion and log, not the whole of my code.
Understood. I completed the code so that I can run it and verify that it works - that information might make for a better question.
OK, thanks. Please let me know if you are getting the error.
I am not getting the error, as indicated in comments above you must have a process hanging onto that port.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.