-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
111 lines (94 loc) · 2.63 KB
/
main.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"log"
"net/http"
"net/http/httputil"
)
// URL is a local server address
const URL = "http://localhost:18888"
// func main() {
// values := url.Values{
// "query": {"hello world"},
// }
// resp, _ := http.Get(URL + "?" + values.Encode())
// defer resp.Body.Close()
// body, _ := ioutil.ReadAll(resp.Body)
// log.Println(string(body))
// }
// x-www-form-urlencoded形式
// func main() {
// values := url.Values{
// "test": {"value"},
// }
// resp, _ := http.PostForm(URL, values)
// body, _ := ioutil.ReadAll(resp.Body)
// log.Println(string(body))
// defer resp.Body.Close()
// }
// func main() {
// file, _ := os.Open("main.go")
// resp, _ := http.Post(URL, "text/plain", file)
// log.Println(resp.StatusCode)
// }
// func main() {
// reader := strings.NewReader("テキスト")
// resp, _ := http.Post(URL, "text/plain", reader)
// log.Println(resp.Status)
// }
// multipart/form-data形式
// func main() {
// var buffer bytes.Buffer
// writer := multipart.NewWriter(&buffer)
// writer.WriteField("name", "Jack")
// fileWriter, _ := writer.CreateFormFile("sample", "main.go")
// readFile, _ := os.Open("main.go")
// defer readFile.Close()
// io.Copy(fileWriter, readFile)
// writer.Close()
// log.Println(writer.FormDataContentType())
// resp, _ := http.Post(URL, writer.FormDataContentType(), &buffer)
// log.Println(resp.Status)
// }
// func main() {
// jar, _ := cookiejar.New(nil)
// client := http.Client{
// Jar: jar,
// }
// for i := 0; i < 2; i++ {
// resp, _ := client.Get(URL + "/cookie")
// dump, _ := httputil.DumpResponse(resp, true)
// log.Println(string(dump))
// }
// }
// プロキシサーバを経由してリクエストを送る
// func main() {
// proxyURL, _ := url.Parse(URL)
// client := http.Client{
// Transport: &http.Transport{
// Proxy: http.ProxyURL(proxyURL),
// },
// }
// resp, _ := client.Get("http://github.com")
// dump, _ := httputil.DumpResponse(resp, true)
// log.Println(string(dump))
// }
// func main() {
// transport := &http.Transport{}
// transport.RegisterProtocol("file", http.NewFileTransport(http.Dir(".")))
// client := http.Client{
// Transport: transport,
// }
// resp, _ := client.Get("file://./main.go")
// dump, _ := httputil.DumpResponse(resp, true)
// log.Println(string(dump))
// }
// GET,HEAD,POST以外はRequestを作成する必要がある
func main() {
client := http.Client{}
request, _ := http.NewRequest("DELETE", URL, nil)
request.Header.Add("X-HOGE", "hoge")
request.SetBasicAuth("username", "password")
resp, _ := client.Do(request)
dump, _ := httputil.DumpResponse(resp, true)
log.Println(string(dump))
}