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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog