@kjk1/

gobook http client get json

Go

No description

fork
loading
Files
  • main.go

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.9521661851322172","path":"main.go","file":{"path":"main.go","content":{"asEncoding":{"base64":"Ly8gbm8gcGxheWdyb3VuZApwYWNrYWdlIG1haW4KCmltcG9ydCAoCgkiZW5jb2RpbmcvanNvbiIKCSJmbXQiCgkiaW8vaW91dGlsIgoJImxvZyIKCSJuZXQvaHR0cCIKCSJuZXQvdXJsIgoJInRpbWUiCikKCi8vIDpzaG93IHN0YXJ0CnR5cGUgcG9zdEl0ZW0gc3RydWN0IHsKCVNjb3JlIGludCAgICBganNvbjoic2NvcmUiYAoJTGluayAgc3RyaW5nIGBqc29uOiJsaW5rImAKfQoKdHlwZSBwb3N0c1R5cGUgc3RydWN0IHsKCUl0ZW1zIFtdcG9zdEl0ZW0gYGpzb246Iml0ZW1zImAKfQoKLy8gOnNob3cgZW5kCmZ1bmMgbWFpbigpIHsKCS8vIDpzaG93IHN0YXJ0Cgl2YWx1ZXMgOj0gdXJsLlZhbHVlc3sKCQkib3JkZXIiOiBbXXN0cmluZ3siZGVzYyJ9LAoJCSJzb3J0IjogIFtdc3RyaW5neyJhY3Rpdml0eSJ9LAoJCSJzaXRlIjogIFtdc3RyaW5neyJzdGFja292ZXJmbG93In0sCgl9CgoJLy8gVVJMIHBhcmFtZXRlcnMgY2FuIGFsc28gYmUgcHJvZ3JhbW1hdGljYWxseSBzZXQKCXZhbHVlcy5TZXQoInBhZ2UiLCAiMSIpCgl2YWx1ZXMuU2V0KCJwYWdlc2l6ZSIsICIxMCIpCgoJdXJpIDo9ICJodHRwczovL2FwaS5zdGFja2V4Y2hhbmdlLmNvbS8yLjIvcG9zdHM/IgoJY2xpZW50IDo9ICZodHRwLkNsaWVudHsKCQlUaW1lb3V0OiAxNSAqIHRpbWUuU2Vjb25kLAoJfQoJcmVzcCwgZXJyIDo9IGNsaWVudC5HZXQodXJpICsgdmFsdWVzLkVuY29kZSgpKQoJaWYgZXJyICE9IG5pbCB7CgkJbG9nLkZhdGFsZigiaHR0cC5HZXQoKSBmYWlsZWQgd2l0aCAnJXMnXG4iLCBlcnIpCgl9CglkZWZlciByZXNwLkJvZHkuQ2xvc2UoKQoKCWlmIHJlc3AuU3RhdHVzQ29kZSAhPSBodHRwLlN0YXR1c09LIHsKCQlkLCBfIDo9IGlvdXRpbC5SZWFkQWxsKHJlc3AuQm9keSkKCQlsb2cuRmF0YWxmKCJSZXF1ZXN0IHdhcyAnJXMnICglZCkgYW5kIG5vdCBPSyAoMjAwKS4gQm9keTpcbiVzXG4iLCByZXNwLlN0YXR1cywgcmVzcC5TdGF0dXNDb2RlLCBzdHJpbmcoZCkpCgl9CgoJZGVjIDo9IGpzb24uTmV3RGVjb2RlcihyZXNwLkJvZHkpCgl2YXIgcCBwb3N0c1R5cGUKCWVyciA9IGRlYy5EZWNvZGUoJnApCglpZiBlcnIgIT0gbmlsIHsKCQlsb2cuRmF0YWxmKCJkZWMuRGVjb2RlKCkgZmFpbGVkIHdpdGggJyVzJ1xuIiwgZXJyKQoJfQoKCWZtdC5QcmludGxuKCJUb3AgMTAgbW9zdCByZWNlbnRseSBhY3RpdmUgU3RhY2tPdmVyZmxvdyBwb3N0czoiKQoJZm10LlByaW50bG4oIlNjb3JlIiwgIkxpbmsiKQoJZm9yIF8sIHBvc3QgOj0gcmFuZ2UgcC5JdGVtcyB7CgkJZm10LlByaW50bG4ocG9zdC5TY29yZSwgcG9zdC5MaW5rKQoJfQoJLy8gOnNob3cgZW5kCn0="},"asBuffer":null},"loaded":true}}
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// no playground
package main

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

// :show start
type postItem struct {
	Score int    `json:"score"`
	Link  string `json:"link"`
}

type postsType struct {
	Items []postItem `json:"items"`
}

// :show end
func main() {
	// :show start
	values := url.Values{
		"order": []string{"desc"},
		"sort":  []string{"activity"},
		"site":  []string{"stackoverflow"},
	}

	// URL parameters can also be programmatically set
	values.Set("page", "1")
	values.Set("pagesize", "10")

	uri := "https://api.stackexchange.com/2.2/posts?"
	client := &http.Client{
		Timeout: 15 * time.Second,
	}
	resp, err := client.Get(uri + values.Encode())
	if err != nil {
		log.Fatalf("http.Get() failed with '%s'\n", err)
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		d, _ := ioutil.ReadAll(resp.Body)
		log.Fatalf("Request was '%s' (%d) and not OK (200). Body:\n%s\n", resp.Status, resp.StatusCode, string(d))
	}

	dec := json.NewDecoder(resp.Body)
	var p postsType
	err = dec.Decode(&p)
	if err != nil {
		log.Fatalf("dec.Decode() failed with '%s'\n", err)
	}

	fmt.Println("Top 10 most recently active StackOverflow posts:")
	fmt.Println("Score", "Link")
	for _, post := range p.Items {
		fmt.Println(post.Score, post.Link)
	}
	// :show end
}