티스토리 뷰
resq, _ := http.Get("http:127.0.0.1/test/" + id);
resp := ioutil.ReadAll(resq.Body)
위 코드를 실행하면
리스폰스 값을 텍스트로 볼수있다
페이지 같은것을 render 하는 거라면 위 코드 이후
return c.HTMLBlob(http상태코드, resp);
위와 같이 리턴후 클라이언트에서 받아서
$("body").html(response);
화면에 그려준다
json으로 값을 받으려면 받을 구조체를 미리 생성해두고
type testStruct struct {
test1 string `json:"test1_key"`
test2 string `json:"test2_key"`
}
resq, _ := http.Get("http:127.0.0.1/test/" + id);
요청후 받은 json데이터에
`json:"test1_key"`
test1_key라는 key가 존재해야한다 ex) test1_key : 13123213
testData := testStruct{}
json.Unmarshal(resp, &testData)
fmt.Printf("json data : %s\n", testData.test1)
fmt.Printf("json data : %s\n", testData.test2)
위와 같이 Unmarshal을 이용해 json데이터를 추출하여 testData에 담을수 있다
return c.JSON(http상태코드, testData);
위와 같이 리턴할수있다
'Go' 카테고리의 다른 글
[Go] url을 string으로 변경 (0) | 2019.09.08 |
---|---|
[Go] Command Line Argument (0) | 2019.08.13 |
[Go] echo 웹 프레임워크 (1) | 2019.08.13 |
[Go] Go 공부사이트 (0) | 2019.08.13 |
[Go] fmt 패키지 (0) | 2019.08.13 |