These are chat archives for gin-gonic/gin

7th
Nov 2017
Derek Robati
@drobati
Nov 07 2017 16:01
So I'm not sure what I'm doing wrong.
type ServiceGroup struct {
    id    string `form:"id" json:"id" binding:"required"`
    value string `form:"type" json:"type" binding:"required"`
}
authorized.POST("", func(c *gin.Context) {
        var data ServiceGroup
        log.Printf("Type: %T, Value: %s", data, data)

        if c.ShouldBind(&data) == nil {
            log.Printf("Type: %T, Value: %s", data.id, data.id)
            log.Printf("Type: %T, Value: %s", data.value, data.value)
            // Can you pull it from the database
            _, ok := DB[data.id]
            if ok {
                // CONFLICT
                c.JSON(http.StatusConflict, gin.H{"reason": "Service Group Exists"})
            } else {
                // CREATE
                DB[data.id] = data.value
                c.JSON(http.StatusCreated, gin.H{"id": data.id, "type": data.value})
            }
        } else {
            c.JSON(http.StatusBadRequest, gin.H{"reason": "Value not defined."})
        }
    })
My test keeps failing because it successfully is able to POST
but then I realized
that there is no data
Type: main.ServiceGroup, Value: { }
Type: string, Value: 
Type: string, Value:
Derek Robati
@drobati
Nov 07 2017 16:06
e.POST("/api/service_groups").
         WithBasicAuth("derek", "zoolander").
         WithQuery("type", "std").
         // WithJSON(map[string]interface{}{"id": "derek", "type": "std"}).
         Expect().
         Status(http.StatusBadRequest)

e.POST("/api/service_groups").
         WithBasicAuth("derek", "zoolander").
         WithQuery("id", "derek").
         Expect().
         Status(http.StatusBadRequest)

e.POST("/api/service_groups").
         WithBasicAuth("derek", "zoolander").
         WithQuery("id", "derek").
         WithQuery("type", "std").
         Expect().
         Status(http.StatusOK)
Essentially, I felt it should fail because the struct ServiceGroup has two required tags for it's fields id and value.
On the first two requests, one of these is not provided.
I thought that the c.ShouldBind() also would identify which type of context-type was being used.
Derek Robati
@drobati
Nov 07 2017 16:12
And as I commented out in my test, I tried to use a different content-type: WithJSON