Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all content of POST HTML form in Gin

Tags:

go

go-gin

I've an HTML form:

<body>
<div>
  <form method="POST" action="/add"name="submitForm">
      <label>Message</label><input type="text" name="message" value="" />
      <input type="checkbox" name="complete" value=""> Complete<br>
      <input type="submit" value="submit" />
  </form>
</div>
</body>

Now I want to access both values (message and complete) at the same time when a POST occurs.

How can I do this in Go (gin gonic)?

I can do it in two times by using c.PostForm but is there a way to collect them both?:

func AddTodoHandler(c *gin.Context) {
    fmt.Println(c.PostForm("message"))
    fmt.Println(c.PostForm("complete"))

I tried fmt.Println(c.PostForm("submitForm")) but that did not work.

like image 947
DenCowboy Avatar asked Oct 25 '25 05:10

DenCowboy


1 Answers

I don't know if you've already solved this, but the way I managed to capture the 2 values simultaneously was by removing the value from the checkbox and using the Request.PostForm instead of the PostForm.

I tried using the form's name or id, but it seems that it's still not possible to get the data from them.

<body>
<div>
  <form method="POST" action="/add" name="submitForm"> << not work
      <label>Message</label><input type="text" name="message" value="" />
      <input type="checkbox" name="complete"> Complete<br> << remove value attribute
      <input type="submit" value="submit" />
  </form>
</div>
</body>
func AddTodoHandler(c *gin.Context) {
    fmt.Println(c.Request.PostForm)

Note: as my reputation is less than 50pts, I cannot comment on the post.

like image 74
Lucas Dittrich Avatar answered Oct 27 '25 01:10

Lucas Dittrich