일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |
- openpyxl
- kitura
- 28015
- port forwarding
- ftp
- PYTHON
- SWIFT
- insert
- nmap
- postgresql
- mysql
- sshpass
- STF_PortForwarding
- ubuntu
- Jupyter Notebook
- appium server
- appium
- 실행권한
- postgres
- create table
- STF
- Materials
- Jupyter
- centos
- GoCD
- nGrinder
- rethinkdb
- nohup
- ssh
- perfect
- Today
- Total
don't stop believing
Querystring parameters 본문
url의 ? 뒤에 따라오는 Query String 처리 입니다.
이번 예제는 ? 뒤에 firstname 과 lastname이 map 형태의 parameter로 전달되게 됩니다.
https://github.com/gin-gonic/gin#querystring-parameters
123456789101112131415161718192021222324252627282930package mainimport ("github.com/gin-gonic/gin")func setupRouter() *gin.Engine {// Disable Console Color// gin.DisableConsoleColor()r := gin.Default()r.GET("/welcome", welcomeQueryString)return r}// Query string parameters are parsed using the existing underlying request object.// The request responds to a url matching: /welcome?firstname=Jane&lastname=Doefunc welcomeQueryString(c *gin.Context) {firstname := c.DefaultQuery("firstname", "Guest")lastname := c.Query("lastname") // shortcut for c.Request.URL.Query().Get("lastname")c.JSON(200, gin.H{"firstname": firstname, "lastname": lastname})}func main() {r := setupRouter()// Listen and Server in 0.0.0.0:8080r.Run(":8080")}
서버의 router에 GET method로 /welcome 이라는 경로를 지정했습니다.
handler 함수로는 welcomeQueryString을 지정했습니다.
func welcomeQueryString(c *gin.Context) {
firstname := c.DefaultQuery("firstname", "Guest")
lastname := c.Query("lastname")
c.JSON(200, gin.H{"firstname": firstname, "lastname": lastname})
}
QueryString을 DefaultQuery() 또는 Query() 함수로 받아올수 있습니다. 이름에서도 보이듯이 DefaultQuery() 함수는 parameter의 값이 없을 경우 "Guest"를 기본값으로 가져옵니다.
이제 실행하고 확인해 보겠습니다.
123456789$ go run 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 /welcome --> main.welcomeQueryString (3 handlers)[GIN-debug] Listening and serving HTTP on :8080
먼저 정상적으로 아래처럼 firstname과 lastname 모두 보내보겠습니다.
http://localhost:8080/welcome?firstname=tongchun&lastname=kim
정상적으로 json 타입의 값이 확인됩니다.
이번에는 firstname과 lastname 안에 값이 없도록 하겠습니다.
http://localhost:8080/welcome?firstname=&lastname=
결과 값에도 firstname과 lastname이 빈 값으로 들어옵니다.
이번에는 firstname과 lastname을 넘겨주지 않겠습니다. /welcome 까지만 호출해 보겠습니다.
welcomeQueryString() handler 함수에서 정의한것 처럼 firstname의 값이 없을 경우 default로 Guest를 지정하도록 되어 있습니다.
결과값에서도 lastname은 빈 값으로 오고, firstname에는 default값인 Guest가 오고 있습니다.
여기까지 QueryString 처리였습니다.
'Golang > Gin' 카테고리의 다른 글
Another example: query + post form (0) | 2019.02.15 |
---|---|
Multipart/Urlencoded Form (1) | 2019.02.14 |
Parameters in path (0) | 2019.02.13 |
Using GET, POST, PUT, PATCH, DELETE and OPTIONS (0) | 2019.02.13 |
Gin 설치와 기본 example 확인해 보기 (0) | 2019.02.13 |