8

I got something running with the Goji framework:

package main

import (
        "fmt"
        "net/http"

        "github.com/zenazn/goji"
        "github.com/zenazn/goji/web"
)

func hello(c web.C, w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hello, %s!", c.URLParams["name"])
}

func main() {
        goji.Get("/hello/:name", hello)
        goji.Serve()
}

What I was hoping someone could help me do is figure out how when an HTML form is submitted to send that data to Golang code.

So if there is an input field with the name attribute and the value of that is name and the user types a name in there and submits, then on the form submitted page the Golang code will print hello, name.

Here is what I could come up with:

package main

import(
    "fmt"
    "net/http"

    "github.com/zenazn/goji"
    "github.com/zenazn/goji/web"
)

func hello(c web.C, w http.ResponseWriter, r *http.Request){
    name := r.PostFormValue("name")
    fmt.Fprintf(w, "Hello, %s!", name)
}

func main(){
    goji.Handle("/hello/", hello)
    goji.Serve()
}

and here is my hello.html file:

in the body:

<form action="" method="get">
    <input type="text" name="name" />
</form>

How do I connect hello.html to hello.go so that the Golang code gets what is in the input and returns hello, name in the form submitted page?

I'd greatly appreciate any and all help!

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
gramme.ninja
  • 1,341
  • 3
  • 11
  • 11

2 Answers2

20

In order to read html form values you have to first call r.ParseForm(). The you can get at the form values.

So this code:

func hello(c web.C, w http.ResponseWriter, r *http.Request){
    name := r.PostFormValue("name")
    fmt.Fprintf(w, "Hello, %s!", name)
}

Should be this:

func hello(c web.C, w http.ResponseWriter, r *http.Request){

    //Call to ParseForm makes form fields available.
    err := r.ParseForm()
    if err != nil {
        // Handle error here via logging and then return            
    }

    name := r.PostFormValue("name")
    fmt.Fprintf(w, "Hello, %s!", name)
}

Edit: I should note that this was a point that tripped me up when learning the net/http package

yumaikas
  • 979
  • 13
  • 24
  • You can also use gorilla/schema (http://www.gorillatoolkit.org/pkg/schema) to marshal a form into a struct or map in one hit. – elithrar Apr 25 '14 at 13:38
  • Just the ticket! This had me stumped for an hour or two this morning, it is not overly obvious for those used to having to parse the URL to get the parameters. – bjpelcdev Jan 31 '15 at 10:02
  • 6
    You actually don't have to call `ParseForm` if you use `FormValue` or `PostFormValue`: "PostFormValue calls ParseMultipartForm and ParseForm if necessary and ignores any errors returned by these functions." https://golang.org/pkg/net/http/#Request.PostFormValue – dchest May 26 '15 at 12:45
6

Your form input name, name is the key to be fetched by go program.

<form action="" method="get">
    <input type="text" name="name" />
</form>

You can use FormValue https://golang.org/pkg/net/http/#Request.FormValue

FormValue returns the first value for the named component of the query. POST and PUT body parameters take precedence over URL query string values. FormValue calls ParseMultipartForm and ParseForm if necessary and ignores any errors returned by these functions. If key is not present, FormValue returns the empty string. To access multiple values of the same key, call ParseForm and then inspect Request.Form directly.

func hello(c web.C, w http.ResponseWriter, r *http.Request){
    name := r.FormValue("name")
    fmt.Fprintf(w, "Hello, %s!", name)
}

If FormFile doesn't works, better use ParseMultiForm https://golang.org/pkg/net/http/#Request.ParseMultipartForm

You can use ParseMultipartForm

ParseMultipartForm parses a request body as multipart/form-data. The whole request body is parsed and up to a total of maxMemory bytes of its file parts are stored in memory, with the remainder stored on disk in temporary files. ParseMultipartForm calls ParseForm if necessary. After one call to ParseMultipartForm, subsequent calls have no effect

func hello(c web.C, w http.ResponseWriter, r *http.Request){
    name := r.FormValue("name")
    r.ParseMultipartForm(32 << 20)
    fmt.Fprintf(w, "Hello, %s!", name)
}

Also, a form is useless unless some kind of processing takes place after the form is submitted. So use it accordingly.

bender
  • 369
  • 1
  • 4
  • 9
Sourabh Bhagat
  • 1,691
  • 17
  • 20