dongmi0760 2015-03-12 07:33
浏览 13

Golang html GET表单方法值未填充

I have a server code and a html form to search a string. Server handler gets the string and search for the same. But I am facing two issues here.

1.Method name is always GET even after I made it as POST.

2.I am Not able to receive the form value in the server end

Server code is here,

package main

import (

var (
    addr = flag.Bool("addr", false, "find open address and print to final-port.txt")

type Page struct {
    Title string
    Body  []byte

type UserInfo struct {
    Title string
    UserId   string
    UserName string

func (p *Page) save() error {
    filename := "projects/" + p.Title + ".txt"
    return ioutil.WriteFile(filename, p.Body, 0600)

func loadPage(title string) (*Page, error) {
    filename := "projects/" + title + ".txt"
    body, err := ioutil.ReadFile(filename)
    if err != nil {
        return nil, err
    return &Page{Title: title, Body: body}, nil

//Home page handler
//Hard coding the user name
func homeHandler(w http.ResponseWriter, r *http.Request, title string) {
    p := &UserInfo{Title: "Project Tube",UserId: "dxa132330", UserName: "Dinesh Appavoo"}
    renderTemplate(w, "home", p)

//Search project handler
func searchHandler(w http.ResponseWriter, r *http.Request, title string) {
    fmt.Println("method:", r.Method) //get request method
    if r.Method == "GET" {
    form_data := r.FormValue("form_data")
    fmt.Println("Form Data : ",form_data)
    fmt.Println("Form Data  1: ",r.Form)
    for _,val := range r.FormValue("search_string") {
        fmt.Println("Search string: ", val)

    } else {
        fmt.Println("Search string:", r.FormValue("search_string"))
    p := &UserInfo{Title: "Project Tube",UserId: "dxa132330", UserName: "Dinesh Appavoo"}
    renderTemplate(w, "searchproject", p)

var templates = template.Must(template.ParseFiles("home.html", "editproject.html", "viewproject.html", "searchproject.html", "header.html", "footer.html"))

func renderTemplate(w http.ResponseWriter, tmpl string, p interface{}) {

    //If you use variables other than the struct u r passing as p, then "multiple response.WriteHeader calls" error may occur. Make sure you pass 
    //all variables in the struct even they are in the header.html embedded 
    if err := templates.ExecuteTemplate(w, tmpl+".html", p); err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)

//URL validation
var validPath = regexp.MustCompile("^/(home|editproject|saveproject|viewproject|searchproject)/(|[a-zA-Z0-9]+)$")

func makeHandler(fn func(http.ResponseWriter, *http.Request, string)) http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {
        m := validPath.FindStringSubmatch(r.URL.Path)
        if m == nil {
            http.NotFound(w, r)
        fn(w, r, m[2])

func main() {
    http.HandleFunc("/home/", makeHandler(homeHandler))
    http.HandleFunc("/searchproject/", makeHandler(searchHandler))
    http.Handle("/resources/", http.StripPrefix("/resources/", http.FileServer(http.Dir("resources"))))

    if *addr {
        l, err := net.Listen("tcp", "")
        if err != nil {
        err = ioutil.WriteFile("final-port.txt", []byte(l.Addr().String()), 0644)
        if err != nil {
        s := &http.Server{}

    http.ListenAndServe(":8080", nil)

I am facing issues in the searchHandler function. And my html code is here

{{ template "header.html" . }}
<div class="container">
    <form action="/searchproject" method="GET">
        <div class="form-group">
            <input type="text" class="form-control" name="search_string">
        <button type="submit" class="btn btn-success">Search</button>

server console log is as follows,

method: GET
Form Data :
Form Data  1:  map[]

Could anyone help me on this? Thanks.

  • 写回答



      相关推荐 更多相似问题


      • ¥15 实值特征权重向量和特征选择,多分类
      • ¥15 INVEST年产水量模块结果出处理
      • ¥15 Neo4j 不显示关系
      • ¥15 这段代码有什么bug
      • ¥15 pip默认安装位置修改不成功
      • ¥15 C++ inline、staitc、const三合一谁能讲通透
      • ¥15 关于pyqt5中QThread的问题
      • ¥20 STM32基于HAL库使用串口发送16进制数组
      • ¥15 OpenCV报错 You can enable it via 'OPENCV_IO_ENABLE_OPENEXR'
      • ¥15 AT89C51的程序问题