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?
How about this?
I've found solution with calling
ParseMultipartForm
beforeParseForm
, it works for me.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.Now your
r.From
is a map of all values your client provided. To extract a particular value you can user.FormValue("<your param name>")
orr.Form.Get("<your param name>")
.So basically you will have this:
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.
I had similar issues using ParseForm. I ended up doing something like this:
Hope that helps!