Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 | 30 |
Tags
- centos
- GoCD
- postgres
- kitura
- 실행권한
- ssh
- ftp
- nohup
- STF_PortForwarding
- nmap
- appium
- Materials
- 28015
- ubuntu
- PYTHON
- Jupyter
- rethinkdb
- port forwarding
- perfect
- appium server
- Jupyter Notebook
- nGrinder
- SWIFT
- mysql
- create table
- openpyxl
- postgresql
- STF
- insert
- sshpass
Archives
- Today
- Total
don't stop believing
Only Bind Query String 본문
어떤 Method로 오건 Query String 데이터를 binding하려면 ShouldBindQuery()를 사용하면 됩니다.
https://github.com/gin-gonic/gin#only-bind-query-string
아래 간단한 예제 코드가 있습니다.
r.Any()에 ShouldBindQuery()를 사용했습니다.
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.ShouldBindQuery(&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") }
POST method에 json 데이터를 보내는데 Query String도 같이 보내봅니다.
POST method로 json 데이터를 보내더라도 ShouldBindQuery()를 사용해 데이터를 binding하기 때문에 Query String이 리턴됩니다.
'Golang > Gin' 카테고리의 다른 글
Bind Uri (0) | 2019.03.14 |
---|---|
How to write log file (0) | 2019.03.14 |
Bind Query String or Post Data (0) | 2019.03.09 |
Model binding and validation (0) | 2019.03.06 |
mysql curd (with Gin) (0) | 2019.03.06 |
Comments