Reputation: 7932
I have a server endpoint in gin that receives an byte array.
func UploadImageHandler(c *gin.Context) {
body, err := c.GetRawData()
// how do I make sure this body byte array is image?
}
I need to ensure that the byte array is an image.
How would I be able to do this check in Go?
Upvotes: 1
Views: 1020
Reputation: 5056
I think you can do this : https://golang.org/pkg/image/#Decode
You will end up with :
func UploadImageHandler(c *gin.Context) {
body, err := c.GetRawData()
img, _, err := image.Decode(bytes.NewReader(body))
if err != nil {
....
}
I have not tested though.
Upvotes: 2