# Request JSON

Untuk request json bisa digunakan method ShouldBind seperti *code* dibawah ini

```go
package main

import (
	"net/http"
	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()
	r.POST("/", PostJSON)
	r.Run(":5000")
}

type User struct {
	Username string
	Password string
}

func PostJSON(ctx *gin.Context) {
	var user User
	if err := ctx.ShouldBind(&user); err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"status":  http.StatusBadRequest,
			"message": err,
		})
	} else {
		ctx.JSON(http.StatusOK, gin.H{
			"status": 	http.StatusOK,
			"data":		map[string]interface{}{
				"username": user.Username,
				"password": user.Password,	
			},
		})
	}
}
```

<figure><img src="https://1578455751-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F09pucuEBc5BTaaur3uoo%2Fuploads%2FkG3k7SMNsGQvAjf5iO3q%2FRes.png?alt=media&#x26;token=47321272-7edf-43ac-a6a9-40d992eda1a0" alt=""><figcaption></figcaption></figure>

Untuk request dengan tipe array JSON perlu tipe data slice sebagai penampungnya.

```go
package main

import (
	"net/http"
	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()
	r.POST("/", PostJSON)
	r.Run(":5000")
}

type User struct {
	Username string
	Password string
}

func PostJSON(ctx *gin.Context) {
	var user []User
	if err := ctx.ShouldBind(&user); err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"status":  http.StatusBadRequest,
			"message": err,
		})
	} else {
		ctx.JSON(http.StatusOK, gin.H{
			"status": 	http.StatusOK,
			"data":		user,
		})
	}
}
```

<figure><img src="https://1578455751-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F09pucuEBc5BTaaur3uoo%2Fuploads%2FfeZteWYnria7wa09h2hR%2F1.png?alt=media&#x26;token=33ac0e58-ae9f-4a22-89b5-f6074930e8e2" alt=""><figcaption></figcaption></figure>
