requests.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. "net"
  7. "net/http"
  8. "net/url"
  9. "strconv"
  10. "time"
  11. )
  12. func startServer() {
  13. http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  14. fmt.Fprintf(w, "getHandler: incoming request %#v\n", r)
  15. fmt.Fprintf(w, "getHandler: r.Url %#v\n", r.URL)
  16. })
  17. http.HandleFunc("/raw_body", func(w http.ResponseWriter, r *http.Request) {
  18. body, err := ioutil.ReadAll(r.Body)
  19. defer r.Body.Close() // важный пункт!
  20. if err != nil {
  21. http.Error(w, err.Error(), 500)
  22. return
  23. }
  24. fmt.Fprintf(w, "postHandler: raw body %s\n", string(body))
  25. })
  26. fmt.Println("starting server at :8080")
  27. http.ListenAndServe(":8080", nil)
  28. }
  29. func runGet() {
  30. url := "http://127.0.0.1:8080/?param=123&param2=test"
  31. resp, err := http.Get(url)
  32. if err != nil {
  33. fmt.Println("error happend", err)
  34. return
  35. }
  36. defer resp.Body.Close() // важный пункт!
  37. respBody, err := ioutil.ReadAll(resp.Body)
  38. fmt.Printf("http.Get body %#v\n\n\n", string(respBody))
  39. }
  40. func runGetFullReq() {
  41. req := &http.Request{
  42. Method: http.MethodGet,
  43. Header: http.Header{
  44. "User-Agent": {"coursera/golang"},
  45. },
  46. }
  47. req.URL, _ = url.Parse("http://127.0.0.1:8080/?id=42")
  48. req.URL.Query().Set("user", "rvasily")
  49. resp, err := http.DefaultClient.Do(req)
  50. if err != nil {
  51. fmt.Println("error happend", err)
  52. return
  53. }
  54. defer resp.Body.Close() // важный пункт!
  55. respBody, err := ioutil.ReadAll(resp.Body)
  56. fmt.Printf("testGetFullReq resp %#v\n\n\n", string(respBody))
  57. }
  58. func runTransportAndPost() {
  59. transport := &http.Transport{
  60. DialContext: (&net.Dialer{
  61. Timeout: 30 * time.Second,
  62. KeepAlive: 30 * time.Second,
  63. DualStack: true,
  64. }).DialContext,
  65. MaxIdleConns: 100,
  66. IdleConnTimeout: 90 * time.Second,
  67. TLSHandshakeTimeout: 10 * time.Second,
  68. ExpectContinueTimeout: 1 * time.Second,
  69. }
  70. client := &http.Client{
  71. Timeout: time.Second * 10,
  72. Transport: transport,
  73. }
  74. data := `{"id": 42, "user": "rvasily"}`
  75. body := bytes.NewBufferString(data)
  76. url := "http://127.0.0.1:8080/raw_body"
  77. req, _ := http.NewRequest(http.MethodPost, url, body)
  78. req.Header.Add("Content-Type", "application/json")
  79. req.Header.Add("Content-Length", strconv.Itoa(len(data)))
  80. resp, err := client.Do(req)
  81. if err != nil {
  82. fmt.Println("error happend", err)
  83. return
  84. }
  85. defer resp.Body.Close() // важный пункт!
  86. respBody, err := ioutil.ReadAll(resp.Body)
  87. fmt.Printf("runTransport %#v\n\n\n", string(respBody))
  88. }
  89. func main() {
  90. go startServer()
  91. time.Sleep(100 * time.Millisecond)
  92. runGet()
  93. runGetFullReq()
  94. runTransportAndPost()
  95. }