-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpbin.go
87 lines (75 loc) · 1.93 KB
/
httpbin.go
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Example demonstrating use of package napping, with HTTP Basic
// Get over HTTP, to exploit http://httpbin.org/.
//
// Implemented the following as demo:
//
// Get -> http://httpbin.org/user-agent
// Get -> http://httpbin.org/get?var=12345
package main
import (
"fmt"
"github.com/jmcvetta/napping"
"log"
)
func init() {
log.SetFlags(log.Ltime | log.Lshortfile)
}
type ResponseUserAgent struct {
Useragent string `json:"user-agent"`
}
// A Params is a map containing URL parameters.
type Params map[string]string
func main() {
//
// Struct to hold error response
//
e := struct {
Message string
}{}
// Start Session
s := napping.Session{}
url := "http://httpbin.org/user-agent"
fmt.Println("URL:>", url)
res := ResponseUserAgent{}
resp, err := s.Get(url, nil, &res, nil)
if err != nil {
log.Fatal(err)
}
//
// Process response
//
println("")
fmt.Println("response Status:", resp.Status())
if resp.Status() == 200 {
// fmt.Printf("Result: %s\n\n", resp.response)
// resp.Unmarshal(&e)
fmt.Println("res:", res.Useragent)
} else {
fmt.Println("Bad response status from Github server")
fmt.Printf("\t Status: %v\n", resp.Status())
fmt.Printf("\t Message: %v\n", e.Message)
}
fmt.Println("--------------------------------------------------------------------------------")
println("")
url = "http://httpbin.org/get"
fmt.Println("URL:>", url)
fooParams := napping.Params{"foo": "bar"}
p := fooParams
res = ResponseUserAgent{}
resp, err = s.Get(url, &p, &res, nil)
if err != nil {
log.Fatal(err)
}
//
// Process response
//
println("")
fmt.Println("response Status:", resp.Status())
fmt.Println("--------------------------------------------------------------------------------")
fmt.Println("Header")
fmt.Println(resp.HttpResponse().Header)
fmt.Println("--------------------------------------------------------------------------------")
fmt.Println("RawText")
fmt.Println(resp.RawText())
println("")
}