Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to bind uri and json for the same struct? #94

Open
chengr4 opened this issue Jun 13, 2023 · 1 comment
Open

Is it possible to bind uri and json for the same struct? #94

chengr4 opened this issue Jun 13, 2023 · 1 comment

Comments

@chengr4
Copy link

chengr4 commented Jun 13, 2023

Eg. for updateAccount API, How to bind uri and json for the same struct?

type updateAccountRequest struct {
    ID int64 `uri:"id" binding:"required,min=1"`
    Currency string `json:"currency" binding:"required,currency"`
}

func (server *Server) updateArticle(ctx *gin.Context) {
    var req updateAccountRequest
    if err := ctx.ShouldBindUri(&req); err != nil {
        // send 400 Bad Request to the client
        ctx.JSON(http.StatusBadRequest, errorResponse(err))
        return
    }

    if err := ctx.ShouldBindJSON(&req); err != nil {
        // send 400 Bad Request to the client
        ctx.JSON(http.StatusBadRequest, errorResponse(err))
        return
    }
}

the code above does not work.

Does anyone know? Thanks in advance

@chengr4
Copy link
Author

chengr4 commented Jun 20, 2023

I use result here: gin-gonic/gin#1824 (comment)
but I wonder whether there is a better solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant