Go Gin: verify base64

WBOY
Release: 2024-02-13 11:00:09
forward
1087 people have browsed it

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"`
}
Copy after login

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
}
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!