Skip to content

Context Bind methods are aborting with status code, which is preventing header updates in after request middleware #840

@AlexeyAtIgloo

Description

@AlexeyAtIgloo

I have post request middleware that looks at presence of errors on the context. And is there are errors, I'm creating specific JSON response that complies with our defined contract.

It mostly works for everything as I use c.Abort() method across my application.

I'm trying to use BingJSON method to parse request body, and when it fails it will write 400 error code onto response and prevents me from changing response header later.

Here is where it is happening:

gin/context.go

Line 333 in 32cab50

c.AbortWithError(400, err).SetType(ErrorTypeBind)

For now I have implemented helper function that does safe bind for me:

func SafeJSONBind(obj interface{},c *gin.Context) error {
	if err := binding.JSON.Bind(c.Request, obj); err != nil {
		return err
	}
	return nil
}

But it would be nice to have option to perform bind that does c.Abort() instead of c.AbortWithError()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions