Skip to content

Use Gin's ShouldBindQuery for typed URL query arguments in API #175

@tomachalek

Description

@tomachalek

This will also allow for generating OpenAPI stuff automatically.

type QueryParams struct {
	ID    int    `form:"id" binding:"required,number"`
	Name  string `form:"name" binding:"required"`
	Email string `form:"email" binding:"required,email"`
}

func handleRequest(c *gin.Context) {
	var queryParams QueryParams

	// Bind query parameters to struct and validate
	if err := c.ShouldBindQuery(&queryParams); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions