Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Gin – struct param is validated as null

I have a function here to create post requests and add a new user of type struct into a slice (the data for the API is just running in memory, so therefore no database):

type user struct {
    ID        string `json:"id"`
    FirstName string `json:"first_name"`
    LastName  string `json:"last_name"`
    Email     string `json:"email"`
}

var users = []user{
    {ID: "1", FirstName: "John", LastName: "Doe", Email: "john.doe@email.com"},
}

func createUser(c *gin.Context) {

    var newUser user

    if len(newUser.ID) == 0 {
        c.JSON(http.StatusBadRequest, gin.H{"message": "user id is null"})
        return
    }

    users = append(users, newUser)
    c.JSON(http.StatusCreated, newUser)
}

Eveything worked fine, until i tried to make an ‘if statement’ that checks if an id for a user being sent with a post request is null.

The problem is that the if statement returns true, when it should return false. So if i for example try to make a post request with the following data:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
    "id": "2",
    "first_name": "Jane",
    "last_name": "Doe",
    "email": "jane.doe@email.com"
}

The if statement that checks for an id with the length of 0 will be true, and therefore return a StatusBadRequest. If have also tried this way:

if newUser.ID == "" {
}

But this also returns true when it shouldn’t.

If i remove this check and create the POST request it works just fine, and the new added data will appear when i make a new GET request.

Why do these if statements return true?

>Solution :

When you create a new user object with var newUser user statement, you are just creating an empty user object. You still have to bind the JSON string you are sending into that object. for that, what you need to do is: c.BindJSON(&newUser). full code will be like:

func createUser(c *gin.Context) {

    var newUser user
    err := c.BindJSON(&newUser)
    if err != nil {
        c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
    }

    if len(newUser.ID) == 0 {
        c.JSON(http.StatusBadRequest, gin.H{"message": "user id is null"})
        return
    }

    users = append(users, newUser)
    c.JSON(http.StatusCreated, newUser)
}

you can check this link for an example provided by Gin: https://github.com/gin-gonic/examples/blob/master/basic/main.go#L61

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading