http.newRequest没有发送POST数据。

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

http.newRequest not sending post data

问题

我有以下代码用于向服务器发送POST数据,但服务器在请求中没有检测到任何POST数据。客户端代码:

cookieJar, _ := cookiejar.New(nil)
client := &http.Client{
    Jar: cookieJar,
}
postUrl := os.Args[1]
username := os.Args[2]
password := os.Args[3]
data := url.Values{}
data.Set("username", username)
data.Add("password", password)
data.Add("remember", "false")

r, _ := http.NewRequest("POST", postUrl, bytes.NewBufferString(data.Encode()))
resp, _ := client.Do(r)
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
fmt.Printf("%s\n", string(body))

服务器端代码:

if(isset($_POST['username'], $_POST['password'], $_POST['remember'])){
    echo $username = md5(($_POST['username']));
    echo $password_original = $_POST['password'];
    echo $password = md5(($_POST['password']));
    echo $remember = $_POST['remember'];
}

请注意,这是一个简单的示例代码,可能存在其他问题。你需要确保服务器端和客户端的代码都正确,并且请求的URL和参数都正确设置。

英文:

I have the following code to send post data to a server, but the server is not detecting any post data on the request. Client code:

cookieJar, _ := cookiejar.New(nil)
		client := &http.Client{
			Jar: cookieJar,
		}
		postUrl := os.Args[1]
		username := os.Args[2]
		password := os.Args[3]
		data := url.Values{}
		data.Set("username", username)
		data.Add("password", password)
		data.Add("remember", "false")

		r, _ := http.NewRequest("POST", postUrl, bytes.NewBufferString(data.Encode()))
		resp, _ := client.Do(r)
		defer resp.Body.Close()
		body, err := ioutil.ReadAll(resp.Body)
		fmt.Printf("%s\n", string(body))

Server code:

if(isset($_POST['username'], $_POST['password'], $_POST['remember'])){
	echo $username = md5(($_POST['username']));
	echo $password_original = $_POST['password'];
	echo $password = md5(($_POST['password']));
	echo $remember = $_POST['remember'];
}

答案1

得分: 3

你正在将数据作为请求体中的表单进行发布,但没有指定内容类型。你可以选择将值放在请求查询中:

r.URL.RawQuery = data.Encode()

或者将内容类型更改为"application/x-www-form-urlencoded":

r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
英文:

You're posting the data as a form in the request body without a content type. You either need to put the values in the request query:

r.URL.RawQuery = data.Encode()

or change the Content-Type to "application/x-www-form-urlencoded":

r.Header.Add("Content-Type", "application/x-www-form-urlencoded")

huangapple
  • 本文由 发表于 2015年6月19日 05:45:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/30926340.html
匿名

发表评论

匿名网友

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

确定