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">
          <div>
            <input type="text" placeholder="Email" name="email"></input>
          </div>
          <br /><br />
          <div>
            <input type="password" placeholder="Password" name="password"></input>
          </div>
          <br /><br />
          <input type="submit"></input>
          <br /><br />
        </form>

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:

    name="email"
    name="password"
    

    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 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据