Below is an server written in go.
package main
import (
"fmt"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
fmt.Fprintf(w,"%s",r.Method)
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
How can I extract the POST
data sent to the localhost:8080/something
URL?
Like this:
func handler(w http.ResponseWriter, r *http.Request) {
r.ParseForm() // Parses the request body
x := r.Form.Get("parameter_name") // x will be "" if parameter is not set
fmt.Println(x)
}
Quoting from the documentation of http.Request
// Form contains the parsed form data, including both the URL
// field's query parameters and the POST or PUT form data.
// This field is only available after ParseForm is called.
// The HTTP client ignores Form and uses Body instead.
Form url.Values
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With