출처 : https://api.raritysniper.com/public/collection/cryptopunks/id/8348

 

package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)

type User struct {
Nftid       int     `json:"nftId"`
Rank        int     `json:"rank"`
Rarityscore float64 `json:"rarityScore"`
}

func customGet() {
// Request 객체 생성
req, err := http.NewRequest("GET", "https://api.raritysniper.com/public/collection/cryptopunks/id/8348", nil)
if err != nil {
panic(err)
}

//헤더 추가
req.Header.Add("User-Agent", "Golang-test")

// Client 객체에서 Request 실행
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()

// 결과 출력
bytes, _ := ioutil.ReadAll(resp.Body)
str := string(bytes) //바이트를 문자열로

var u User
json.Unmarshal([]byte(str), &u)
fmt.Printf("%+v\n", u) // {Name:gopher Age:7}

//fmt.Println(str)

}

func main() {
customGet()

}

 

 

 

 

 

 

'go' 카테고리의 다른 글

go 프로젝트 세팅  (0) 2024.02.26
JSON Transfer  (0) 2022.04.14
Web Handler  (0) 2022.04.14
go언어 설치  (0) 2022.04.14
routing module  (0) 2022.04.14
블로그 이미지

iesay

,