我正在使用 Go 语言的 http
包处理 POST 请求。如何从 Request
对象中访问和解析查询字符串的内容?我在官方文档中没有找到答案。
如何使用 Go 语言的 http
包处理 POST 请求?如何从 Request
对象中访问和解析查询字符串的内容?我在官方文档中找不到答案。
我正在使用 Go 语言的 http
包处理 POST 请求。如何从 Request
对象中访问和解析查询字符串的内容?我在官方文档中没有找到答案。
如何使用 Go 语言的 http
包处理 POST 请求?如何从 Request
对象中访问和解析查询字符串的内容?我在官方文档中找不到答案。
以下是如何访问GET参数的更具体示例。 Request
对象有一个名为Query的方法,可以为您解析它们:
假设请求URL如下:http://host:port/something?param1=b
func newHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println("GET params were:", r.URL.Query())
// if only one expected
param1 := r.URL.Query().Get("param1")
if param1 != "" {
// ... process it, will be the first (only) if multiple were given
// note: if they pass in like ?param1=¶m2= param1 will also be "" :|
}
// if multiples possible, or to process empty values like param1 in
// ?param1=¶m2=something
param1s := r.URL.Query()["param1"]
if len(param1s) > 0 {
// ... process them ... or you could just iterate over them without a check
// this way you can also tell if they passed in the parameter as the empty string
// it will be an element of the array that is the empty string
}
}
还要注意,“Values映射中的键[即Query()返回值中的键]区分大小写。”
r.URL.Query().Get("moviename")
避免这种致命错误)。 - Dave CGet
方法返回第一个,因此这是更多示例。有用的信息,谢谢! - rogerdpackValues.Get()
返回的空字符串与nil
进行比较。
https://golang.org/pkg/net/url/#Values - erik258req.URL
(doc)访问请求的URL。URL对象具有一个Query()
方法(doc),该方法返回一个Values
类型,这只是一个QueryString参数的map[string][]string
。ParseForm()
方法调用并使用req.Form
字段获取键值对映射是正确的,但您也可以调用FormValue(key)
方法来获取特定键的值。这会在需要时调用ParseForm()
方法,并获取无论是以查询字符串还是请求正文的方式发送的值。req.URL.RawQuery
返回 GET 请求中问号后的所有内容。 - kouton以下是一个例子:
value := r.FormValue("field")
了解有关HTTP包的更多信息,您可以访问其文档此处。 FormValue
基本上按照POST或PUT值或GET值的顺序返回第一个找到的值。
获得查询参数有两种方式:
在第二种情况下,必须小心,因为请求体参数将覆盖查询参数。有关获取查询参数的完整说明,请参见此处。
https://golangbyexample.com/net-http-package-get-query-params-golang
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)
}
以下文字来自官方文档。
Form 包含解析后的表单数据,包括 URL 字段的查询参数 和 POST 或 PUT 表单数据。只有在调用 ParseForm 后才能使用此字段。
因此,以下示例代码将起作用。
func parseRequest(req *http.Request) error {
var err error
if err = req.ParseForm(); err != nil {
log.Error("Error parsing form: %s", err)
return err
}
_ = req.Form.Get("xxx")
return nil
}
r.FormValue("id")
来获取查询参数,那么你不能通过cURL的表单数据发送它(例如,curl 0.0.0.0:8888 -d id=foobar
将不起作用)。你必须通过查询参数发送它(curl 0.0.0.0:8888?id=foobar
)。 - user427390