request.param(…)在Go语言中的位置在哪里?

huangapple go评论69阅读模式
英文:

Where is request.param(...) in Go

问题

这是我的控制台:

GET http://localhost:8080/api/photos.json?token=ABCDEFGHIJKLMNOPQRSTUVWXYZ

200 OK
	0	
jquery.js (第8526行)
|参数|    头部    响应    JSON
token	ABCDEFGHIJKLMNOPQRSTUVWXYZ

我在参数选项卡中。我如何访问它,并将token日志记录到我的终端窗口中。

在Node.js中:request.param('token')

英文:

Here is my console:

GET http://localhost:8080/api/photos.json?token=ABCDEFGHIJKLMNOPQRSTUVWXYZ

200 OK
	0	
jquery.js (line 8526)
|Params|    Headers    Response    JSON
token	ABCDEFGHIJKLMNOPQRSTUVWXYZ

I am in the params tab. How do I access this and, for example log token to my terminal window.

In node: request.param('token')

答案1

得分: 2

只需使用func (*Request) FormValue

> 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
英文:

Just use func (*Request) FormValue

>FormValue returns the first value for the named component of the query. POST and PUT body parameters take precedence over URL query string values. FormValue calls ParseMultipartForm and ParseForm if necessary. To access multiple values of the same key use ParseForm.

Simple Server

package main

import (
	&quot;fmt&quot;
	&quot;net/http&quot;
)

func main() {
	http.HandleFunc(&quot;/&quot;, home)
	http.ListenAndServe(&quot;:4000&quot;, nil)

}

func home(w http.ResponseWriter , r *http.Request) {
	fmt.Fprint(w, &quot;&lt;html&gt;&lt;body&gt;&lt;h1&gt;Hello &quot;, r.FormValue(&quot;token&quot;) , &quot;&lt;/h1&gt;&lt;/body&gt;&lt;/html&gt;&quot;)
}

Visit localhost:4000/photos.json?token=ABCDEFGHIJKLMNOPQRSTUVWXYZ you would get

Hello ABCDEFGHIJKLMNOPQRSTUVWXYZ

答案2

得分: 0

我假设你有一个 http.Request 对象,假设它被称为 hr

然后你可以执行以下操作:

hr.ParseForm()

之后,你可以使用 hr.Form,它的定义如下:

// Form 包含解析后的表单数据,包括 URL 字段的查询参数和 POST 或 PUT 表单数据。
// 只有在调用 ParseForm 后,该字段才可用。
// HTTP 客户端会忽略 Form 字段,而使用 Body 字段。
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 ""
}
英文:

I suppose you have a http.Request. Let's suppose it's called hr.

Then you can do

hr.ParseForm()

and after that you may use hr.Form which is defined like this :

// Form contains the parsed form data, including both the URL
// field&#39;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

where url.Values is a map :

type Values map[string][]string

Here's an example of use of a parsed form where I'm only interested in the first value for a given name :

func getFormValue(hr *http.Request, name string) string {
	if values := hr.Form[name]; len(values) &gt; 0 {
		return values[0]
	}
	return &quot;&quot;
}

huangapple
  • 本文由 发表于 2013年8月5日 19:58:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/18057607.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定