http.newRequest不发送帖子数据

时间:2015-06-18 21:45:25

标签: php go

我有以下代码将发布数据发送到服务器,但服务器未检测到请求中的任何发布数据。客户代码:

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'];
}

1 个答案:

答案 0 :(得分:2)

您将数据作为表单发布在请求正文中而没有内容类型。您需要将值放在请求查询中:

r.URL.RawQuery = data.Encode()

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

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