duanliao3826 2019-02-02 19:21
浏览 142
已采纳

如何发出具有多个参数的发布请求

How could I make a POST request to take in multiple parameters and output info on a webpage, in Go using the standard library.

i.e

user puts in name and favorite hobby

Name :

Hobby :

Submit (button)

then webpage updates and shows

Your Name is (Name) and you like to (Hobby)

  • 写回答

1条回答 默认 最新

  • doudao1369 2019-02-02 20:25
    关注

    You can do this using the html/template package in the Go standard library.

    The basic process here is:

    1. Write a template (in go/HTML template language)
    2. Read in the template (using template.ParseFiles or similar)
    3. Listen for requests
    4. Pass information from relevant requests to your template (using ExecuteTemplate or similar)

    You can pass a struct to ExecuteTemplate, which is then accessible in the template you define (see below for an example). For example, if your struct has a field called Name, then you can access this information in the template with {{ .Name }}.

    Here is a sample:

    main.go:

    package main

    import (
        "log"
        "encoding/json"
        "html/template"
        "net/http"
    )
    
    var tpl *template.Template
    
    func init() {
        // Read your template(s) into the program
        tpl = template.Must(template.ParseFiles("index.gohtml"))
    }
    
    func main() {
        // Set up routes
        http.HandleFunc("/endpoint", EndpointHandler)
        http.ListenAndServe(":8080", nil)
    }
    
    // define the expected structure of payloads
    type Payload struct {
        Name string     `json:"name"`
        Hobby string    `json:"hobby"`
    }
    
    func EndpointHandler(w http.ResponseWriter, r *http.Request) {
        // Read the body from the request into a Payload struct
        var payload Payload
        err := json.NewDecoder(r.Body).Decode(&payload)
        if err != nil {
            log.Fatal(err)
        }
    
        // Pass payload as the data to give to index.gohtml and write to your ResponseWriter
        w.Header().Set("Content-Type", "text/html")
        tpl.ExecuteTemplate(w, "index.gohtml", payload)
    }
    

    index.gohtml:

    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
    </head>
    <body>
        <div>
            <span>Your name is</span><span>{{ .Name }}</span>
        </div>
    
        <div>
            <span>Your hobby is</span><span>{{ .Hobby }}</span>
        </div>
    </body>
    </html>
    

    Sample:

    With payload:

    {
        "name": "Ahmed",
        "hobby": "devving"
    }
    

    Response:

    <!DOCTYPE html>
    <html>
        <head>
            <title></title>
        </head>
        <body>
            <div>
                <span>Your name is</span>
                <span>Ahmed</span>
            </div>
            <div>
                <span>Your hobby is</span>
                <span>devving</span>
            </div>
        </body>
    </html>
    

    Note this is pretty fragile, so you should definitely add better error and edge-case handling, but hopefully this is a helpful starting point.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码