golang http服务器,无法获得发布价值 [英] golang http server,can't get post value

查看:138
本文介绍了golang http服务器,无法获得发布价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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,欢迎回来,%s ,req.FormValue(userid))
}
}

和我使用ASIhttprequst发送一个,像这样。

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

我用req.FormValue(userid)得到了一个空值

发生了什么?以及如何解决这个问题?

解决方案

如何解决这个问题?

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


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?

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

这篇关于golang http服务器,无法获得发布价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆