这是我的控制台:
GET http://localhost:8080/api/photos.json?token=ABCDEFGHIJKLMNOPQRSTUVWXYZ
200 OK
0
jquery.js (line 8526)
|Params| Headers Response JSON
token ABCDEFGHIJKLMNOPQRSTUVWXYZ
我在params标签中。如何访问此内容,例如将日志标记发送到终端窗口。
在node:request.param('token')
中答案 0 :(得分:2)
FormValue返回查询的命名组件的第一个值。 POST和PUT正文参数优先于URL查询字符串值。如有必要,FormValue会调用ParseMultipartForm和ParseForm。要访问同一密钥的多个值,请使用ParseForm。
简单服务器
package main
import (
"fmt"
"net/http"
)
func main() {
http.HandleFunc("/", home)
http.ListenAndServe(":4000", nil)
}
func home(w http.ResponseWriter , r *http.Request) {
fmt.Fprint(w, "<html><body><h1>Hello ", r.FormValue("token") , "</h1></body></html>")
}
访问localhost:4000/photos.json?token=ABCDEFGHIJKLMNOPQRSTUVWXYZ
您将获得
Hello ABCDEFGHIJKLMNOPQRSTUVWXYZ
答案 1 :(得分:0)
我想你有http.Request
。我们假设它被称为hr
。
然后你可以做
hr.ParseForm()
之后您可以使用hr.Form
,其定义如下:
// Form contains the parsed form data, including both the URL
// field's query parameters and the POST or PUT form data.
// This field is only available after ParseForm is called.
// The HTTP client ignores Form and uses Body instead.
Form url.Values
其中url.Values
是地图:
type Values map[string][]string
以下是使用已解析表单的示例,其中我只对给定名称的第一个值感兴趣:
func getFormValue(hr *http.Request, name string) string {
if values := hr.Form[name]; len(values) > 0 {
return values[0]
}
return ""
}