For this, you can use c.ShouldBindBodyWith.

  1. objA := formA{}
  2. objB := formB{}
  3. // This reads c.Request.Body and stores the result into the context.
  4. if errA := c.ShouldBindBodyWith(&objA, binding.JSON); errA == nil {
  5. c.String(http.StatusOK, `the body should be formA`)
  6. c.String(http.StatusOK, `the body should be formB JSON`)
  7. // And it can accepts other formats
  8. } else if errB2 := c.ShouldBindBodyWith(&objB, binding.XML); errB2 == nil {
  9. c.String(http.StatusOK, `the body should be formB XML`)
  10. ...
  11. }
  12. }
  • 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 multiple times without any damage to
    performance (See ).