dpca4790 2018-02-21 15:21
浏览 241

带有Golang / Gin后端的POST HTML表单

I am trying to POST a simple HTML form to my backend, which uses Golang and Gin. Here is the HTML:

<form action="/login" method="post" name="loginForm">
            <input type="text" placeholder="Email" name="email"></input>
          <br /><br />
            <input type="password" placeholder="Password" name="password"></input>
          <br /><br />
          <input type="submit"></input>
          <br /><br />

And here is the route from my main.go:

r.POST("/login", func(c *gin.Context) {
        //What do I need to put here?
        formContent := c.PostForm("loginForm")

        c.JSON(200, gin.H{
            "status":  "posted to login",
            "message": "whoo",
            "form": formContent})

When I submit the form, I receive the JSON response, but formContent is an empty string. I'm guessing I'm using the wrong method on c, but I'm not too experienced with Golang so not sure what to replace it with. I'm also not sure what additional capabilities Gin introduces here, as I'm essentially learning with trial and error from an example project.

How do I properly access the form data on the backend?

  • 写回答

1条回答 默认 最新

  • duanfei1987 2018-02-21 15:27

    Your form:


    Your server-side code:

    formContent := c.PostForm("loginForm")

    You are looking for a field which does not exist.

    emailValue := c.PostForm("email");
    passwordValue := c.PostForm("password");
    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 速度位置规划实现精确定位的问题
  • ¥15 代码问题:df = pd.read_excel('c:\User\18343\Desktop\wpsdata.xlxs')路径读不到
  • ¥15 为什么视频算法现在全是动作识别?
  • ¥15 编写一段matlab代码
  • ¥15 用Python做岩石类别鉴定软件
  • ¥15 关于调取、提交更新数据库记录的问题
  • ¥15 之前删了盘从下vs2022遇见这个问题 搞了一整天了
  • ¥15 从Freecad中宏下载的DesignSPHysics,出现如下问题是什么原因导致的(语言-python)
  • ¥30 notepad++ 自定义代码补全提示
  • ¥15 MATLAB有限差分法解一维边值问题