@@ -2,112 +2,58 @@ package utils
22
33import (
44 "fmt"
5- "io/ioutil"
6- // "log"
7- "net/http"
85
9-
6+ "github.com/valyala/fasthttp"
107)
118
12- var statusMessage = []string {
13- 100 : "Continue" ,
14- 101 : "Switching Protocols" ,
15- 102 : "Processing" ,
16- 103 : "Early Hints" ,
17- 200 : "OK" ,
18- 201 : "Created" ,
19- 202 : "Accepted" ,
20- 203 : "Non-Authoritative Information" ,
21- 204 : "No Content" ,
22- 205 : "Reset Content" ,
23- 206 : "Partial Content" ,
24- 207 : "Multi-Status" ,
25- 208 : "Already Reported" ,
26- 226 : "IM Used" ,
27- 300 : "Multiple Choices" ,
28- 301 : "Moved Permanently" ,
29- 302 : "Found" ,
30- 303 : "See Other" ,
31- 304 : "Not Modified" ,
32- 305 : "Use Proxy" ,
33- 306 : "Switch Proxy" ,
34- 307 : "Temporary Redirect" ,
35- 308 : "Permanent Redirect" ,
36- 400 : "Bad Request" ,
37- 401 : "Unauthorized" ,
38- 402 : "Payment Required" ,
39- 403 : "Forbidden" ,
40- 404 : "Not Found" ,
41- 405 : "Method Not Allowed" ,
42- 406 : "Not Acceptable" ,
43- 407 : "Proxy Authentication Required" ,
44- 408 : "Request Timeout" ,
45- 409 : "Conflict" ,
46- 410 : "Gone" ,
47- 411 : "Length Required" ,
48- 412 : "Precondition Failed" ,
49- 413 : "Request Entity Too Large" ,
50- 414 : "Request URI Too Long" ,
51- 415 : "Unsupported Media Type" ,
52- 416 : "Requested Range Not Satisfiable" ,
53- 417 : "Expectation Failed" ,
54- 418 : "I'm a teapot" ,
55- 421 : "Misdirected Request" ,
56- 422 : "Unprocessable Entity" ,
57- 423 : "Locked" ,
58- 424 : "Failed Dependency" ,
59- 426 : "Upgrade Required" ,
60- 428 : "Precondition Required" ,
61- 429 : "Too Many Requests" ,
62- 431 : "Request Header Fields Too Large" ,
63- 451 : "Unavailable For Legal Reasons" ,
64- 500 : "Internal Server Error" ,
65- 501 : "Not Implemented" ,
66- 502 : "Bad Gateway" ,
67- 503 : "Service Unavailable" ,
68- 504 : "Gateway Timeout" ,
69- 505 : "HTTP Version Not Supported" ,
70- 506 : "Variant Also Negotiates" ,
71- 507 : "Insufficient Storage" ,
72- 508 : "Loop Detected" ,
73- 510 : "Not Extended" ,
74- 511 : "Network Authentication Required" ,
75- }
76-
779func Get (uri string ) error {
78- client := http.Client {}
79- req , err := http .NewRequest ("GET" , uri , nil )
10+ req := fasthttp .AcquireRequest ()
11+ defer fasthttp .ReleaseRequest (req )
12+ req .SetRequestURI (uri )
13+
14+ // Acquire a response instance
15+ resp := fasthttp .AcquireResponse ()
16+ defer fasthttp .ReleaseResponse (resp )
8017
81- resp , err := client .Do (req )
82-
83- body , err := ioutil .ReadAll (resp .Body )
18+ err := fasthttp .Do (req , resp )
8419 if err != nil {
85- panic (err )
20+ fmt .Printf ("Client get failed: %s\n " , err )
21+ return err
8622 }
87- fmt .Println (string (body ))
23+ if resp .StatusCode () != fasthttp .StatusOK {
24+ fmt .Printf ("Expected status code %d but got %d\n " , fasthttp .StatusOK , resp .StatusCode ())
25+ return err
26+ }
27+ body := resp .Body ()
28+
29+ fmt .Printf ("Response body is: %s" , body )
8830
8931 return nil
32+
9033}
9134
92- // func Post(uri string, payload string, content_type string) []byte {
93- // req := fasthttp.AcquireRequest()
94- // defer fasthttp.ReleaseRequest(req)
35+ func Post (uri string , payload string , content_type string ) ( []byte , error ) {
36+ req := fasthttp .AcquireRequest ()
37+ defer fasthttp .ReleaseRequest (req )
9538
96- // resp := fasthttp.AcquireResponse()
97- // defer fasthttp.ReleaseResponse(resp)
39+ resp := fasthttp .AcquireResponse ()
40+ defer fasthttp .ReleaseResponse (resp )
9841
99- // req.Header.SetMethod("POST")
100- // req.Header.SetContentType(content_type)
42+ req .Header .SetMethod ("POST" )
43+ req .Header .SetContentType (content_type )
10144
102- // req.SetRequestURI(uri)
103- // req.SetBody([]byte(payload))
45+ req .SetRequestURI (uri )
10446
105- // if err := fasthttp.Do(req, resp); err != nil {
106- // log.Println("Error: ", err)
107- // }
108- // result := resp.Body()
109- // // fmt.Println(result)
47+ req .SetBody ([]byte (payload ))
11048
111- // return result
49+ err := fasthttp .Do (req , resp )
50+ if err != nil {
51+ fmt .Printf ("Client get failed: %s\n " , err )
52+ return nil , err
53+ }
54+ body := resp .Body ()
55+ fmt .Printf ("Response body is: %s" , body )
11256
113- //}
57+ return body , nil
58+
59+ }
0 commit comments