[GIN-debug] [WARNING] Headers were already written. Wanted to override status code 400 with 200

This error reporting scenario is: 1. Gin framework, 2. Write the POST request mode interface, 3. Use the structure binding mode to accept parameters

The pitfall phenomenon is that when the structure binding parameter fails (i.e., when the parameter is wrong or transmitted incorrectly), the httpcode is 400. No matter how the developer forcibly returns the httpcode, the code is 400

Reason: Behavior – These methods MustBindWith are used under the hood. If there is a binding error, the request is aborted c AbortWithError(400, err). SetType(ErrorTypeBind)。 This sets the response status code to 400 and the Content Type title to text/plain; charset=utf-8。 Please note that if you try to set the response code after this, the warning [GIN debug] [WARNING] Headers were already written Wanted to override status code 400 with 422。 If you want better control over behavior, consider using the ShouldBind equivalent.

Solution: The method of binding structures is not applicable. [BindJSON] is changed to [ShouldBind]

param := make(map[string]interface{})
err := c.ShouldBind(&param)
if err != nil {
photoV, _ := param["photo"].(string) 
photoValue := util.PhotoBase64Start(photoV)

Leave a Reply

Your email address will not be published. Required fields are marked *