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 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。