Was wondering if you could help me with the following issue i have…
when i send a POST request to my httpHandler, it parses the form data fine and displays it… a get method does not.
can someone please explain why, and maybe provide an example? I’m taking it a GET method send a request to the server to receive information… rather than sending form data to the server… but i need a way around this, without sending a post request with a formvalue for method…
code:
func httpHandler(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
k := r.FormValue("key")
v := r.FormValue("value")
dt := r.FormValue("datatype")
fmt.Fprintf(w, "httphandler, %v, %v", k, v)
nk := r.FormValue("newkey")
nv := r.FormValue("newvalue")
fmt.Fprintf(w, "httphandler2, %v, %v", k, v)
switch r.Method {
case http.MethodPost:
handlePost(w, k, v, dt)
case http.MethodGet:
handleGet(w, k, v)
case http.MethodPut:
handlePut(w, k, v, nk, nv, dt)
case http.MethodDelete:
handleGet(w, k, v)
}
}
so when i run this with a get method, it sends nil values to the handleGet(w, k, v) function
Thank you for your reply. In the end i kept the parse form etc for POST requests… but added urv, _ := url.ParseQuery(r.URL.Path) to parse the get params from the URL… then used: handleGet(w, urv.Get("key"), urv.Get("value")). Is this the most efficient method in this scenario?
I am not a performance optimization expert but I think I can claim to see no particular inefficiency here. Parsing the URL query string is required and thus can hardly be replaced. Fetching values from a map is quite efficient (*), so unless your code hits some limits when handling the targeted amount of request per second, I would not worry too much about speed.
(*) Accessing a map of n elements is near O(1) on average and O(n) in the worst case. (Blog article about Go map implementation, and Blog article about Big-O.)