Skip to content

Request body as parameter #2091

@phanithlim

Description

@phanithlim

Hi, could you help with this part? I’m having an issue:

// register godoc
//
//	@Summary		Register a new user
//	@Description	Register a new user
//	@Tags			Auth
//	@Produce		json
//	@Accept			json
//	@Param			payload  body	request.RegisterRequest	true "payload"
//	@Success		201		{object}	map[string]interface{}
//	@Router			/api/v1/auth/register [post]
func (app *application) register(c *gin.Context) {
	req := new(request.RegisterRequest)
	if err := c.BindJSON(&req); err != nil {
		c.JSON(400, gin.H{"error": err.Error()})
		return
	}
	hashedPassword, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
	if err != nil {
		c.JSON(400, gin.H{"error": err.Error()})
	}
	req.Password = string(hashedPassword)
	user := database.User{
		Name:     req.Name,
		Email:    req.Email,
		Password: req.Password,
	}
	if err = app.models.Users.Insert(user); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
	}
	c.JSON(http.StatusCreated, gin.H{"data": user})
}
package request

type RegisterRequest struct {
	Email    string `json:"email" validate:"required" example:"[email protected]"` // User email address
	Name     string `json:"name" validate:"required" example:"Michael Lue"`     // User full name
	Password string `json:"password" validate:"required" example:"123456"`      // User password (minimum 8 characters)
}

When I generate the Swagger docs, the payload is not shown as a request body it show as
. I want it to appear as a proper request body parameter in Swagger.

Image

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