Parse input from HTML <form>

后端 未结 2 1811
天命终不由人
天命终不由人 2020-12-20 16:27

I got something running with the Goji framework:

package main

import (
        \"fmt\"
        \"net/http\"

        \"github.com/zenazn/goji\"
        \"gi         


        
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-20 16:57

    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

提交回复
热议问题