1

I want to get the request "name" to format it before save it. In Laravel, it is easy to get the value with postman via $request->input('name') at the controller.

I learn Go recently and got confused how to get the same value from postman. I am using http/net in Go.

How do I get the $request->input('name') in Go as in Laravel?

I'm working with POST method.

Vinotaz
  • 55
  • 8
  • 1
    Duplicates: https://stackoverflow.com/q/15407719/13860, https://stackoverflow.com/q/47748215/13860 – Jonathan Hall Dec 29 '22 at 13:51
  • Your comment works for GET method. I am using POST method. Doesn't work – Vinotaz Dec 29 '22 at 14:34
  • 5
    It would be prudent to put these details in your question, so we aren't left guessing. For a POST request, you need to parse the body first, by calling [ParseForm](https://pkg.go.dev/net/http#Request.ParseForm), then use [FormValue](https://pkg.go.dev/net/http#Request.FormValue), for example. – Jonathan Hall Dec 29 '22 at 14:44

1 Answers1

0
func handler(w http.ResponseWriter, r *http.Request) {
    r.ParseForm()                     // Parses the request body
    name := r.Form.Get("name") // name will be "" if parameter is not set
    fmt.Println(name)
}
Evan Miao
  • 87
  • 2