Gin: bind to string in post body

Created on 3 Jun 2018  路  1Comment  路  Source: gin-gonic/gin

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?

Most helpful comment

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

>All comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

frederikhors picture frederikhors  路  3Comments

lilee picture lilee  路  3Comments

mastrolinux picture mastrolinux  路  3Comments

gplume picture gplume  路  3Comments

ghost picture ghost  路  3Comments