Try to bind body into different structs

The normal methods for binding request body consumes c.Request.Body and they
cannot be called multiple times.

  1. type formA struct {
  2. Foo string `json:"foo" xml:"foo" binding:"required"`
  3. }
  4. type formB struct {
  5. Bar string `json:"bar" xml:"bar" binding:"required"`
  6. }
  7. func SomeHandler(c *gin.Context) {
  8. objA := formA{}
  9. objB := formB{}
  10. // This c.ShouldBind consumes c.Request.Body and it cannot be reused.
  11. if errA := c.ShouldBind(&objA); errA == nil {
  12. c.String(http.StatusOK, `the body should be formA`)
  13. // Always an error is occurred by this because c.Request.Body is EOF now.
  14. } else if errB := c.ShouldBind(&objB); errB == nil {
  15. c.String(http.StatusOK, `the body should be formB`)
  16. } else {
  17. ...
  18. }
  19. }

For this, you can use c.ShouldBindBodyWith.

  1. func SomeHandler(c *gin.Context) {
  2. objA := formA{}
  3. objB := formB{}
  4. // This reads c.Request.Body and stores the result into the context.
  5. if errA := c.ShouldBindBodyWith(&objA, binding.JSON); errA == nil {
  6. c.String(http.StatusOK, `the body should be formA`)
  7. // At this time, it reuses body stored in the context.
  8. } else if errB := c.ShouldBindBodyWith(&objB, binding.JSON); errB == nil {
  9. c.String(http.StatusOK, `the body should be formB JSON`)
  10. // And it can accepts other formats
  11. } else if errB2 := c.ShouldBindBodyWith(&objB, binding.XML); errB2 == nil {
  12. c.String(http.StatusOK, `the body should be formB XML`)
  13. } else {
  14. ...
  15. }
  16. }
  • c.ShouldBindBodyWith stores body into the context before binding. This has
    a slight impact to performance, so you should not use this method if you are
    enough to call binding at once.
  • This feature is only needed for some formats — JSON, XML, MsgPack,
    ProtoBuf. For other formats, Query, Form, FormPost, FormMultipart,
    can be called by c.ShouldBind() multiple times without any damage to
    performance (See #1341).