Home >Backend Development >Golang >Go Gin: verify base64

Go Gin: verify base64

WBOY
WBOYforward
2024-02-13 11:00:091124browse

Go Gin:验证 base64

php editor Banana today introduces a verification tool called Go Gin. Its function is to verify whether the base64-encoded string is legal. In Internet applications, base64 encoding is one of the commonly used data transmission methods, but sometimes we need to verify the received base64 string to ensure the integrity and security of the data. Go Gin provides a simple and easy-to-use interface, which can help developers quickly perform base64 verification and improve development efficiency and data security. Next, we will introduce the use of Go Gin and related techniques in detail, hoping to be helpful to everyone.

Question content

I'm trying to use gin to send a base64 image to a server written in go. I created a structure with bindings and json tags to represent the request body like this:

type createcompanyrequestbody struct {
    name string `json:"name" binding:"required"`
    size string `json:"size" binding:"required"`
    logo string `json:"logo" binding:"required,base64|base64url|base64rawurl"`
}

When trying to decode the body using gin's shouldbindjson I get an error for the logo field. However, I did verify using an online tool (https://onlinepngtools.com/convert-base64-to-png) that the decoded object does contain a valid base64 string in the logo field.

The code to decode json is as follows:

var body createCompanyRequestBody
if err := ctx.ShouldBindJSON(&body); err != nil {
    ctx.Status(http.StatusBadRequest) // Will always hit this
}

I haven't used gin before, so I'm sure I'm making an oversight, but I can't seem to figure out what. How can I change the structure to allow the base64 variant that is provided as expected?

Workaround

If you send the image as a data URI instead of just sending the naked base64 string, then You should use the datauri validator instead of base64|base64URL|base64RawURL.

The above is the detailed content of Go Gin: verify base64. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete