golang的http服务器,无法获取post的值

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

golang http server,can't get post value

问题

func login(rw http.ResponseWriter, req *http.Request) {
req.ParseForm()

if req.Method == "GET" {
	fmt.Fprintf(rw, "Error Method")
} else {
	name := strings.TrimSpace(req.FormValue("userid"))
	fmt.Println("userid:", name)
	fmt.Println("pwd:", req.FormValue("pwd"))
	fmt.Fprintf(rw, "welcome back,%s", req.FormValue("userid"))
}

}

and i using ASIhttprequst send a from,like this.

[self setRequest:[ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://127.0.0.1:8080/login"]]];
[request setPostValue:@"userid" forKey:@"fdfs@jkjlf.cm"];
[request setPostValue:@"pwd" forKey:@"fdsfdsfdkskfjhds"];
[request setRequestMethod:@"POST"];

i got a null value with req.FormValue("userid")

what happend? and how to fix it?

英文:
func login(rw http.ResponseWriter, req *http.Request) {
	req.ParseForm()

	if req.Method == "GET" {
		fmt.Fprintf(rw, "Error Method")
	} else {
		name := strings.TrimSpace(req.FormValue("userid"))
		fmt.Println("userid:", name)
		fmt.Println("pwd:", req.FormValue("pwd"))
		fmt.Fprintf(rw, "welcome back,%s", req.FormValue("userid"))
	}
}

and i using ASIhttprequst send a from,like this.

[self setRequest:[ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://127.0.0.1:8080/login"]]];
[request setPostValue:@"userid" forKey:@"fdfs@jkjlf.cm"];
[request setPostValue:@"pwd" forKey:@"fdsfdsfdkskfjhds"];
[request setRequestMethod:@"POST"];

i got a null value with req.FormValue("userid")

what happend? and how to fix it?

答案1

得分: 10

这样怎么样?

req.ParseForm()
req.Form.Get(key)
英文:

How about this?

req.ParseForm()
req.Form.Get(key)

答案2

得分: 4

你已经使用req.PostFormValue而不是req.FormValue进行测试了吗?

英文:

did you already test it with req.PostFormValue instead of req.FormValue ?

答案3

得分: 0

我在调用ParseForm之前使用ParseMultipartForm找到了解决方案,对我有效。

英文:

I've found solution with calling ParseMultipartForm before ParseForm, it works for me.

答案4

得分: 0

如果您进行multipart/form-data的POST请求,ParseForm无法正确解析请求体(这可能是一个bug)。所以,如果是这种情况,请使用ParseMultipartForm。

或者,您可以直接调用FormValue或PostFormValue而不调用这些解析方法。

英文:

If you make multipart/form-data POST request, ParseForm does not parse request body correctly (this might be a bug). So, use ParseMultipartForm if that is the case.

Or you can call FormValue or PostFormValue directly without calling these parse methods.

答案5

得分: 0

我在使用ParseForm时遇到了类似的问题。最后我做了这样的处理:

type UserRequest struct {
  UserId  string `json:"userid"`
  Pwd     string `json:"pwd"`
}

func login(w http.ResponseWriter, r *http.Request) {
   var ur UserRequest
   decode := json.NewDecoder(r.Body)
   decoder.Decode(&ur)
  
   // 通过结构体访问数据
}

希望对你有所帮助!

英文:

I had similar issues using ParseForm. I ended up doing something like this:

type UserRequest struct {
  UserId  string `json:"userid"`
  Pwd     string `json:"pwd"`
}

func login(w http.ResponseWriter, r *http.Request) {
   var ur UserRequest
   decode := json.NewDecoder(r.Body)
   decoder.Decode(&ur)
  
   // Access data via struct
}

Hope that helps!

答案6

得分: 0

要从POST请求中提取一个值,首先要调用r.ParseForm()这个会解析URL中的原始查询并更新r.Form。

> 对于POST或PUT请求,它还会将请求体解析为一个表单,并将结果放入r.PostForm和r.Form中。POST和PUT请求体参数优先于r.Form中的URL查询字符串值。

现在你的r.Form是客户端提供的所有值的映射。要提取特定的值,可以使用r.FormValue("<your param name>")r.Form.Get("<your param name>")

所以基本上你会有这样的代码:

r.ParseForm()
res := r.FormValue("<your param name>")
英文:

To extract a value from a post request you have to call r.ParseForm() at first. This parses the raw query from the URL and updates r.Form.

> For POST or PUT requests, it also parses the request body as a form
> and put the results into both r.PostForm and r.Form. POST and PUT body
> parameters take precedence over URL query string values in r.Form.

Now your r.From is a map of all values your client provided. To extract a particular value you can use r.FormValue(&quot;&lt;your param name&gt;&quot;) or r.Form.Get(&quot;&lt;your param name&gt;&quot;).

So basically you will have this:

r.ParseForm()
res := r.FormValue(&quot;&lt;your param name&gt;&quot;)

答案7

得分: 0

// 表单包含解析后的表单数据,包括URL字段的查询参数和POST或PUT表单数据。
// 只有在调用ParseForm之后,该字段才可用。
// HTTP客户端忽略Form字段,而使用Body字段。
表单 url.Values

// PostForm包含来自POST、PATCH或PUT请求体参数的解析后的表单数据。
//
// 只有在调用ParseForm之后,该字段才可用。
// HTTP客户端忽略PostForm字段,而使用Body字段。
PostForm url.Values

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

    // PostForm contains the parsed form data from POST, PATCH,
    // or PUT body parameters.
    //
    // **This field is only available after ParseForm is called.**
    // The HTTP client ignores PostForm and uses Body instead.
    PostForm url.Values

答案8

得分: 0

确保 req.Bodyio.Reader 缓冲区在某个级别上被读取后被清空

例如:使用 ioutil.ReadAll,如果 Body 缓冲区被清空,即使你使用了 ParseFormFormValue 也会返回空值。

英文:

Make sure the req.Body io.Reader buffer is getting cleared by reading it in some level

ex: ioutil.ReadAll if Body buffer got cleared it will return empty value on FormValue even you ParseForm

答案9

得分: -1

试试这个伙计。

希望它能像对我一样有效。

r.FormValue("userid")
英文:

Try this buddy.

hope it will work as it works for me

r.FormValue(&quot;userid&quot;)

huangapple
  • 本文由 发表于 2013年6月3日 20:10:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/16896430.html
匿名

发表评论

匿名网友

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

确定