10 examples of 'golang http test' in Go

Every line of 'golang http test' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
15func main() {
16 links := []string{
17 "http://www.baidu.com",
18 "http://www.jd.com/‎",
19 "https://www.taobao.com/",
20 "https://www.163.com/",
21 "https://www.sohu.com/",
22 }
23
24
25 for _, link := range links {
26 checkLink(link)
27 }
28}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

15func main() {
16 links := []string{
17 "http://www.baidu.com",
18 "http://www.jd.com/‎",
19 "https://www.taobao.com/",
20 "https://www.163.com/",
21 "https://www.sohu.com/",
22 }
23
24 c:=make(chan string)
25 for _, link := range links {
26 go checkLink(link,c)
27 }
28 fmt.Println(<-c)
29 fmt.Println(<-c)
30 fmt.Println(<-c)
31 fmt.Println(<-c)
32 fmt.Println(<-c)
33 fmt.Println(<-c)
34}
10func main() {
11
12 resp, err := http.Get("http://www.golang.org")
13 if err != nil {
14 log.Fatalln("http get error:", err)
15 }
16 defer resp.Body.Close()
17
18 n, err := html.Parse(resp.Body)
19 if err != nil {
20 log.Fatal(err)
21 }
22
23 switch n.Type {
24 case 0:
25 fmt.Println("ErrorNode")
26 case 1:
27 fmt.Println("TextNode")
28 case 2:
29 fmt.Println("DocumentNode")
30 case 3:
31 fmt.Println("ElementNode")
32 case 4:
33 fmt.Println("CommentNode")
34 case 5:
35 fmt.Println("DoctypeNode")
36 case 6:
37 fmt.Println("scopeMarkerNode")
38 }
39 fmt.Println("\t\tType", n.Type)
40 fmt.Println("\t\tData", n.Data)
41 fmt.Println("\t\tNamespace", n.Namespace)
42 fmt.Println("\t\tAttr", n.Attr)
43}
68func long3(c *gin.Context) {
69 // request a slow service
70 // see https://github.com/vearne/gin-timeout/blob/master/example/slow_service.go
71 url := "http://localhost:8882/hello"
72 // Notice:
73 // Please use c.Request.Context(), the handler will be canceled where timeout event happen.
74 req, _ := http.NewRequestWithContext(c.Request.Context(), http.MethodGet, url, nil)
75 client := http.Client{Timeout: 100 * time.Second}
76 resp, err := client.Do(req)
77 if err != nil {
78 // Where timeout event happen, a error will be received.
79 fmt.Println("error1:", err)
80 return
81 }
82 defer resp.Body.Close()
83 s, err := ioutil.ReadAll(resp.Body)
84 if err != nil {
85 fmt.Println("error2:", err)
86 return
87 }
88 fmt.Println(s)
89}
9func main() {
10 res, _ := http.Get("http://www.geekwiseacademy.com/")
11 page, _ := ioutil.ReadAll(res.Body)
12 res.Body.Close()
13 fmt.Printf("%s", page)
14}
9func main() {
10 rsp, err := http.Get("http://127.0.0.1:8000/hello")
11 if err != nil {
12 fmt.Println(err)
13 }
14
15 defer rsp.Body.Close()
16
17 rspbody, err := ioutil.ReadAll(rsp.Body)
18 fmt.Println(string(rspbody), err)
19}
17func main() {
18 start := time.Now()
19 ch := make(chan string)
20 for i := 0; i < 10; i++ {
21 go get("http://localhost:4444/ping", ch)
22 }
23
24 for i := 0; i < 10; i++ {
25 fmt.Println(<-ch)
26 }
27 fmt.Printf("%.2fs saniye geçti\n", time.Since(start).Seconds())
28}
58func main() {
59 proxyURL, _ := url.Parse(PROXY)
60 timeElapsed, err := repeatHTTPReqTime(TARGETURL, proxyURL, TOTALTIMES, DURATIONTIME)
61 if err != nil {
62 log.Println("[ERROR] Request failed:", err)
63 }
64 log.Println("[INFO] Total time elapsed:", timeElapsed)
65 log.Println("[INFO] Average time per request:", timeElapsed/TOTALTIMES)
66}
13func main() {
14 for {
15 res := http.Response{
16 Proto: "HTTP/1.1",
17 ProtoMajor: 1,
18 ProtoMinor: 1,
19 StatusCode: 200,
20 Status: "OK",
21 }
22
23 r := bufio.NewReader(os.Stdin)
24 req, err := http.ReadRequest(r)
25
26 var buf bytes.Buffer
27 if err != nil {
28 res.StatusCode = 500
29 res.Status = http.StatusText(res.StatusCode)
30 fmt.Fprintln(&buf, err)
31 } else {
32 l, _ := strconv.Atoi(req.Header.Get("Content-Length"))
33 p := make([]byte, l)
34 r.Read(p)
35 fmt.Fprintf(&buf, "Hello %s\n", p)
36 for k, vs := range req.Header {
37 fmt.Fprintf(&buf, "ENV: %s %#v\n", k, vs)
38 }
39 }
40
41 res.Body = ioutil.NopCloser(&buf)
42 res.ContentLength = int64(buf.Len())
43 res.Write(os.Stdout)
44 }
45}
92func main() {
93 _, err := http.Get("http://localhost:9922/api/status")
94 if err != nil {
95 log.Fatal(err)
96 }
97
98 urls := []string{
99 "https://hacker-news.firebaseio.com/v0/topstories.json",
100 "https://hacker-news.firebaseio.com/v0/newstories.json",
101 "https://hacker-news.firebaseio.com/v0/askstories.json",
102 "https://hacker-news.firebaseio.com/v0/showstories.json",
103 }
104
105 for _, url := range urls {
106 err = loadList(url)
107 if err != nil {
108 log.Fatal(err)
109 }
110 }
111}

Related snippets