So, my post request looks something like this:
fetch('/endpoint', {method: "POST", body: "my string"})
And, I'm trying to bind the body to a string like this:
var x string
c.Bind(&x)
But I'm getting the error reflect: NumField of non-struct type
.
What is the appropriate way to get the body as a string, or do I need to just use the http Request.Body as a reader?
Comment From: zwhitchcox
Was able to accomplish my goal with 3 lines:
buf := new(bytes.Buffer)
buf.ReadFrom(c.Request.Body)
str := buf.String()
where str is the string containing the post body
Comment From: luga97
Was able to accomplish my goal with 3 lines:
go buf := new(bytes.Buffer) buf.ReadFrom(c.Request.Body) str := buf.String()
where str is the string containing the post body
Why is this not possible with Bind? Out of curiosity.
Comment From: Rospaccio
A possible alternative:
// c *gin.Context
data, _ := c.GetRawData()
text := fmt.Sprintf("%s", data)