일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- Jupyter Notebook
- postgresql
- nGrinder
- port forwarding
- nohup
- openpyxl
- rethinkdb
- Materials
- PYTHON
- kitura
- ftp
- sshpass
- 실행권한
- GoCD
- insert
- SWIFT
- appium server
- STF_PortForwarding
- 28015
- create table
- mysql
- nmap
- centos
- Jupyter
- STF
- appium
- ubuntu
- postgres
- ssh
- perfect
- Today
- Total
don't stop believing
Bind Query String or Post Data 본문
binding은 method에 따라 달라집니다.
ShouldBind() 함수는 GET일때와 POST일때가 다릅니다. GET method일 때는 Query String이 bind로 오고, POST method일 경우 header의 content-type를 확인해 Json인지, XML인지를 확인합니다. 만약 Json도 XML도 아니라면 Form 데이터를 받습니다.
https://github.com/gin-gonic/gin#bind-query-string-or-post-data
아래와 같이 코드를 작성했습니다.
살펴볼 것은 r.Any()를 사용했습니다. 그리고 ShouldBind()를 사용해 binding 합니다.
package main import ( "net/http" "time" "github.com/gin-gonic/gin" ) type User struct { User string `form:"user" json:"user" xml:"user" binding:"required"` Password string `form:"password" json:"password" xml:"password" binding:"required"` Birthday time.Time `form:"birthday" time_format:"2006-01-02" time_utc:"1"` } func setupRouter() *gin.Engine { // Disable Console Color // gin.DisableConsoleColor() r := gin.Default() r.Any("/anymethod", bindingTest) return r } // I don't know how to get the data. func bindingTest(c *gin.Context) { var user User if err := c.ShouldBind(&user); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, gin.H{ "status": "you are logged in", "info": user, }) } func main() { r := setupRouter() // Listen and Server in 0.0.0.0:8080 r.Run(":8080") }
위 코드를 실행하면 아래와 같이 실행됩니다.
$ go run ./example/server.go [GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached. [GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production. - using env: export GIN_MODE=release - using code: gin.SetMode(gin.ReleaseMode) [GIN-debug] GET /anymethod --> main.bindingTest (3 handlers) [GIN-debug] POST /anymethod --> main.bindingTest (3 handlers) [GIN-debug] PUT /anymethod --> main.bindingTest (3 handlers) [GIN-debug] PATCH /anymethod --> main.bindingTest (3 handlers) [GIN-debug] HEAD /anymethod --> main.bindingTest (3 handlers) [GIN-debug] OPTIONS /anymethod --> main.bindingTest (3 handlers) [GIN-debug] DELETE /anymethod --> main.bindingTest (3 handlers) [GIN-debug] CONNECT /anymethod --> main.bindingTest (3 handlers) [GIN-debug] TRACE /anymethod --> main.bindingTest (3 handlers) [GIN-debug] Listening and serving HTTP on :8080
r.Any()로 하면 모든 method를 죄다 실행합니다. handler는 하나로 사용하죠.
이제 GET method로 먼저 보내봅니다.
http://localhost:8080/anymethod?user=tongchun&password=ngle1234&birthday=1076-10-26
GET method로 보낸 Query String이 bind되서 리턴됩니다.
이제 GET의 Query String은 그대로 두고 POST로 보내보겠습니다. header의 Content-Type은 application/json 입니다.
json으로 보낸 데이터가 리턴됩니다.
보통은 어떤 method를 사용할지 그리고 어떤 형식으로 데이터를 보낼지에 따라 binding 함수를 구분해서 사용하는 것이 좋을 것 같습니다.
'Golang > Gin' 카테고리의 다른 글
How to write log file (0) | 2019.03.14 |
---|---|
Only Bind Query String (0) | 2019.03.09 |
Model binding and validation (0) | 2019.03.06 |
mysql curd (with Gin) (0) | 2019.03.06 |
[swag] Gin에서 Swagger 사용하기 (0) | 2019.03.04 |