In Go's http package, how do I get the query string on a POST request?

前端 未结 6 564
滥情空心
滥情空心 2021-01-30 03:59

I\'m using the httppackage from Go to deal with POST request. How can I access and parse the content of the query string from the Requestobject ? I can

6条回答
  •  无人及你
    2021-01-30 04:18

    Here's a simple, working example:

    package main
    
    import (
        "io"
        "net/http"
    )
    func queryParamDisplayHandler(res http.ResponseWriter, req *http.Request) {
        io.WriteString(res, "name: "+req.FormValue("name"))
        io.WriteString(res, "\nphone: "+req.FormValue("phone"))
    }
    
    func main() {
        http.HandleFunc("/example", func(res http.ResponseWriter, req *http.Request) {
            queryParamDisplayHandler(res, req)
        })
        println("Enter this in your browser:  http://localhost:8080/example?name=jenny&phone=867-5309")
        http.ListenAndServe(":8080", nil)
    }
    

提交回复
热议问题