When i am trying to get a value from the json map like this:
...
func SomeFunc(context *gin.Context) {
something := context.GetString("a-key")
...
I always and up with a string looking like ""
Is this a bug or i am just being stupid?
Comment From: carlosm27
Hello. How are you?
No, you are not stupid.
If you want to extract "a-key" from a JSON, this approach works for me:
type Something struct {
Key: string `json: "a-key" binding: "required"`
}
func SomeFunc(context *gin.Context) {
var something Something
if err := c.Bind(&something); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
key := something.Key
}
It is more verbose, I know. Also, I'm assuming this is for a POST handler. If you are using a client to send the json data, set the header value as 'application/json'. When I don't specify the header value I got " "
too. I hope this works for you.
Have a nice day.
Comment From: haithngn
When i am trying to get a value from the json map like this:
... func SomeFunc(context *gin.Context) { something := context.GetString("a-key") ...
I always and up with a string looking like
""
Is this a bug or i am just being stupid?
hi @de-falt-GH, you should check the value set to the context, if you set a string pointer you will get an empty string from the gin context
```