dpvp56187 2015-06-18 21:45
浏览 18
已采纳

http.newRequest不发送帖子数据

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
", 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条回答 默认 最新

  • doupishan3309 2015-06-18 21:55
    关注

    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")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分