json Body를 채워 /postTest로 요청하면 원하는 값으로 변경되어 return 되는 테스트
route.go
func (server *serverStruct) SetRoute() bool {
server.route.POST("/postTest", handler.PostTest)
return true
}
핸들러 추가
package handler
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/gin-gonic/gin"
)
type Board struct {
ID int `json:"ID"`
Title string `json:"Title"`
Content string `json:"Content"`
}
func PostTest(c *gin.Context) {
c.Header("Content-Type", "application/json charset=utf-8")
var jsons Board
//요청 바디에 있는 값을 Board 형식에 맞게 바꾼다.
err := json.NewDecoder(c.Request.Body).Decode(&jsons)
if err != nil {
fmt.Println(err)
}
//값을 변경한다.
jsons.Title = "hey"
jsons.Content = "hi"
fmt.Println(jsons)
//변경된 값을 보낸다.
c.JSON(200, jsons)
}
'GoLang' 카테고리의 다른 글
GIN 그래프 그리기 (0) | 2020.11.19 |
---|---|
GIN MongoDB Test (0) | 2020.11.17 |
GIN POST 테스트 (0) | 2020.11.12 |
리눅스 서버에 GIN 포팅하기 (0) | 2020.11.11 |
GoLang (0) | 2020.11.05 |