異なる構造体へのボディのバインドを試す
リクエストボディをバインドするための通常のメソッドは`c.Request.Body`を消費し、複数回呼び出すことはできません。
type formA struct {
Foo string `json:"foo" xml:"foo" binding:"required"`
}
type formB struct {
Bar string `json:"bar" xml:"bar" binding:"required"`
}
func SomeHandler(c *gin.Context) {
objA := formA{}
objB := formB{}
// This c.ShouldBind consumes c.Request.Body and it cannot be reused.
if errA := c.ShouldBind(&objA); errA == nil {
c.String(http.StatusOK, `the body should be formA`)
// Always an error is occurred by this because c.Request.Body is EOF now.
} else if errB := c.ShouldBind(&objB); errB == nil {
c.String(http.StatusOK, `the body should be formB`)
} else {
...
}
}
このためには、`c.ShouldBindBodyWith`を使用することができます。
func SomeHandler(c *gin.Context) {
objA := formA{}
objB := formB{}
// This reads c.Request.Body and stores the result into the context.
if errA := c.ShouldBindBodyWith(&objA, binding.JSON); errA == nil {
c.String(http.StatusOK, `the body should be formA`)
// At this time, it reuses body stored in the context.
} else if errB := c.ShouldBindBodyWith(&objB, binding.JSON); errB == nil {
c.String(http.StatusOK, `the body should be formB JSON`)
// And it can accepts other formats
} else if errB2 := c.ShouldBindBodyWith(&objB, binding.XML); errB2 == nil {
c.String(http.StatusOK, `the body should be formB XML`)
} else {
...
}
}
- `c.ShouldBindBodyWith`は、バインド前にボディをコンテキストに保存します。これはパフォーマンスにわずかな影響を与えるため、一度にバインディングを呼び出すだけで十分な場合は、このメソッドを使用しないでください。
- この機能は、`JSON`、`XML`、`MsgPack`、`ProtoBuf`など、一部のフォーマットにのみ必要です。その他のフォーマット、`Query`、`Form`、`FormPost`、`FormMultipart`は、パフォーマンスに影響を与えることなく`c.ShouldBind()`で複数回呼び出すことができます(#1341を参照)。