윤준석

ADD: sample api request implementation

1 package main 1 package main
2 2
3 +import (
4 + "fmt"
5 + "io/ioutil"
6 + "joongna/config"
7 + "log"
8 + "net/http"
9 + url2 "net/url"
10 +)
11 +
3 func main() { 12 func main() {
13 + keyword := "m1 pro 맥북 프로 16인치"
14 + encText := url2.QueryEscape("중고나라" + keyword)
15 + url := "https://openapi.naver.com/v1/search/cafearticle.json?query=" + encText + "&sort=sim"
16 +
17 + req, err := http.NewRequest("GET", url, nil)
18 + if err != nil {
19 + log.Fatal(err)
20 + }
21 + req.Header.Add("X-Naver-Client-Id", config.Cfg.Secret.CLIENTID)
22 + req.Header.Add("X-Naver-Client-Secret", config.Cfg.Secret.CLIENTSECRET)
23 +
24 + client := &http.Client{}
25 + resp, err := client.Do(req)
26 + if err != nil {
27 + log.Fatal(err)
28 + }
29 + defer resp.Body.Close()
30 +
31 + bytes, _ := ioutil.ReadAll(resp.Body)
32 + str := string(bytes)
33 + fmt.Println(str)
4 } 34 }
......