일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- Materials
- ssh
- SWIFT
- mysql
- openpyxl
- STF
- nohup
- postgres
- 실행권한
- STF_PortForwarding
- postgresql
- port forwarding
- GoCD
- perfect
- nmap
- kitura
- Jupyter Notebook
- create table
- insert
- centos
- ftp
- sshpass
- appium server
- 28015
- ubuntu
- PYTHON
- Jupyter
- appium
- rethinkdb
- nGrinder
- Today
- Total
don't stop believing
Using GET, POST, PUT, PATCH, DELETE and OPTIONS 본문
[https://github.com/gin-gonic/gin#using-get-post-put-patch-delete-and-options]
각 http method에 대해 확인해 보겠습니다.
요청되는 method에 따라 handler 함수를 지정할 수 있습니다.
참고로 http method에 대한 간략한 설명은 아래 링크에서 확인할 수 있습니다.
https://www.tutorialspoint.com/http/http_methods.htm
바로 예제 코드를 확인해 보겠습니다.
1234567891011121314151617181920212223242526272829303132package mainimport ("github.com/gin-gonic/gin")func setupRouter() *gin.Engine {// Disable Console Color// gin.DisableConsoleColor()r := gin.Default()r.GET("/someGet", someMethod)r.POST("/somePost", someMethod)r.PUT("/somePut", someMethod)r.DELETE("/someDelete", someMethod)r.PATCH("/somePatch", someMethod)r.HEAD("/someHead", someMethod)r.OPTIONS("/someOptins", someMethod)return r}func main() {r := setupRouter()// Listen and Server in 0.0.0.0:8080r.Run(":8080")}func someMethod(c *gin.Context) {httpMethod := c.Request.Methodc.JSON(200, gin.H{"status": "good", "sending": httpMethod})}
처음 setupRouter라는 함수를 만들어 줍니다. return 타입은 gin.Engine의 포인터입니다.
gin.Default를 r에 초기화 합니다. 그럼 r에 각 http method를 지정할 수 있습니다.
main() 함수에선 setupRouter() 함수를 r 변수에 대입합니다.
r.Run()으로 서버를 실행시킵니다. 포트 번호를 지정하기 위해 ":8080"을 대입했습니다. 만약 아무것도 입력하지 않는다면 3000번 포트가 기본으로 설정됩니다.
setupRouter()함수 안에서 정의한 라우터에 대해 someMethod()라는 handler 함수를 대입했습니다.
someMethod()에서는 요청된 method를 확인해 json 타입의 데이터를 반환하게 됩니다.
이제 실행시켜 보겠습니다.
1234567891011121314151617$ go run main.go[GIN-debug] [WARNING] Now Gin requires Go 1.6 or later and Go 1.7 will be required soon.[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 /someGet --> main.someMethod (3 handlers)[GIN-debug] POST /somePost --> main.someMethod (3 handlers)[GIN-debug] PUT /somePut --> main.someMethod (3 handlers)[GIN-debug] DELETE /someDelete --> main.someMethod (3 handlers)[GIN-debug] PATCH /somePatch --> main.someMethod (3 handlers)[GIN-debug] HEAD /someHead --> main.someMethod (3 handlers)[GIN-debug] OPTIONS /someOptins --> main.someMethod (3 handlers)[GIN-debug] Listening and serving HTTP on :8080
브라우저를 열고 get method에 대해 확인해 봅니다.
정상적으로 실행이 되는군요.
get 이외의 method를 확인하기 위해 postman을 사용해 보겠습니다.
잘 넘어오고 있습니다.
'Golang > Gin' 카테고리의 다른 글
Another example: query + post form (0) | 2019.02.15 |
---|---|
Multipart/Urlencoded Form (1) | 2019.02.14 |
Querystring parameters (0) | 2019.02.14 |
Parameters in path (0) | 2019.02.13 |
Gin 설치와 기본 example 확인해 보기 (0) | 2019.02.13 |