0

I am trying to turn off handling GET requests in golang. I just want to handle POST.

Is it possible to do?

Reason for doing so is that i can see more and more memory being allocated by golang whenever i go to localhost:8080 and refresh page multiple times.

Here is my test code:

package main

import (
  "fmt"
  "net/http"
  "encoding/json"
)
type test_struct struct {
    Test string 
}
var t test_struct

func handlePOST(rw http.ResponseWriter, req *http.Request) {
    switch req.Method {
    case "POST":
    decoder := json.NewDecoder(req.Body)
    decoder.Decode(&t)
    defer req.Body.Close()
    fmt.Println(t.Test)
    }
}
func main() {
    http.HandleFunc("/", handlePOST)
    http.ListenAndServe(":8080", nil)
}
6
  • So what's the problem? Commented Sep 5, 2017 at 9:16
  • I can see Golang handling GET requests even as function is not defined to do so. Whenever i go to the port of my program i can see memory being allocated which increases memory usage. I would like to avoid this behavior . Commented Sep 5, 2017 at 9:19
  • 1
    "i can see memory being allocated which increases memory usage" Probably not. Finding leaked memory is hard. Looking at the process monitor or top is not helpful. So what's the problem? Commented Sep 5, 2017 at 9:26
  • By default all kinds of requests are being handled in http.HandleFunc("/", handlePOST). Can i turn it off in some easy way? Commented Sep 5, 2017 at 9:30
  • 1
    @Mac No, you can't. What you're doing is the best way: do nothing in the handler if HTTP method is not POST. Commented Sep 5, 2017 at 10:54

1 Answer 1

2

You cannot not handle GET requests, Go's HTTP server (or rather its http.ServeMux) only allows you to specify a path pattern before dispatching the request to your handler. HTTP method related routing can only happen at the handler level.

Note that some external mux libraries allow you to register handlers to specific HTTP methods only, but the decision and routing based on that also happens in "hidden" handlers of those libraries.

What you're doing is the best: simply do nothing in the handler if the HTTP method is not the one you intend to handle, or even better: send back a http.StatusMethodNotAllowed error response:

func myHandler(w http.ResponseWriter, r *http.Request) {
    if r.Method != http.MethodPost {
        http.Error(w, "Only POST is allowed", http.StatusMethodNotAllowed)
        return
    }

    var t test_struct // Use local var not global, else it's a data race
    decoder := json.NewDecoder(r.Body)
    if err := decoder.Decode(&t); err != nil {
        fmt.Println("Error decoding:", err)
    }
    fmt.Println(t.Test)
}
Sign up to request clarification or add additional context in comments.

Comments

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.