0

I have updated gin-gonic and want to use it to save a form.

Here is my code:

import (
    "fmt"
    "github.com/gin-gonic/contrib/sessions"
    "github.com/gin-gonic/gin"
...


func HandlePost(c *gin.Context) {
    userId, userName := getUserId(c)

    title := c.PostForm("title")
    content := c.PostForm("content")

    fmt.Println("userId is:", userId) //ok
    fmt.Println("userName is: ", userName) //ok
    fmt.Println("title is:", title) //empty
    fmt.Println("content is: ", content) //empty

...

I use Postman to post form, like this:

enter image description here

I don't get any errors but received form fields are empty. So wondering what could be wrong here and how to fix it?

Update: here is how I implemented getUserId:

func getUserId(c *gin.Context) (int, string) {
    var user []model.User
    session := sessions.Default(c)
    userEmail := session.Get("user-id").(string)
    if userEmail == "" {
        fmt.Println("userEmail NOT found")
    } else {
        err := shared.Dbmap.Select(&user, "SELECT * FROM user WHERE email = ? LIMIT 1", userEmail)
        if err != nil {
            log.Panic(err)
        }
    }
    return user[0].Id, user[0].UserName
}
Karlom
  • 13,323
  • 27
  • 72
  • 116
  • How did you implement `getUserId`? I guess, when the function `getUserId` is called, it read all the request body till `EOF` so when you access the *title* and *content*, you got empty value. – putu Jul 17 '17 at 16:11
  • @putu, I added `getUserId` function to the question. But even if when I comment out t call to `getUserId`, the form fields are still empty. – Karlom Jul 17 '17 at 16:14
  • 1
    So my initial guess was wrong, since in `getUserId` you don't read the request body. How if you try to read the form with [Context.MultipartForm() (*multipart.Form, error)](https://godoc.org/github.com/gin-gonic/gin#Context.MultipartForm) and observe the `error`? You may also try to [inspect the Postman request](http://blog.getpostman.com/2015/06/13/debugging-postman-requests/) to see what exactly was sent to the server. – putu Jul 17 '17 at 16:40
  • Well, I followed the official gin docs to read the form post. How can I use `c.MultipartForm` instead? – Karlom Jul 17 '17 at 16:42
  • 1
    Add `form, err := c.MultipartForm()` before `title := c.PostForm("title")`. Observe the content and error by `fmt.Printf("%+v, %+v\n", form, err)`. If there was no error, you can read the value from [multipart.Form.Value](https://godoc.org/mime/multipart#Form). – putu Jul 17 '17 at 16:46
  • I did. And now I get `, invalid URL escape "%\x00\x00" ` – Karlom Jul 17 '17 at 16:54
  • Now at least we understand why you got empty value. How if you remove `file` field in the Postman request? Please also inspect the postman request to see what was exactly being sent to the server. The error message indicates that the request was invalid. – putu Jul 17 '17 at 17:05
  • Right. removing file from post does not have any effect. I'm still struggling how to inspect the post. Can you give me curl equivalent (including cookie) so that I bypass Postman? – Karlom Jul 17 '17 at 17:09
  • 1
    You can access and inspect the request through [history](https://www.getpostman.com/docs/postman/sending_api_requests/history). For cURL and cookie, [https://stackoverflow.com/questions/15995919/curl-how-to-send-cookies-via-command-line](https://stackoverflow.com/questions/15995919/curl-how-to-send-cookies-via-command-line) – putu Jul 17 '17 at 17:16
  • 1
    Holy moly! When I post using curl, the form values are retrieves. Thanks a lot putu for solving this puzzle! Answer and I'll accept. I should ditch Postman! Here is the curl: `curl -X POST -v --cookie "mycookename=somelonggibberish" -F 'title=mytitle' -F 'content=mycontent' http://127.0.0.1:8080/api/post` – Karlom Jul 17 '17 at 17:27

0 Answers0