client_example_test.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package fasthttp_test
  2. import (
  3. "log"
  4. "github.com/valyala/fasthttp"
  5. )
  6. func ExampleHostClient() {
  7. // Perpare a client, which fetches webpages via HTTP proxy listening
  8. // on the localhost:8080.
  9. c := &fasthttp.HostClient{
  10. Addr: "localhost:8080",
  11. }
  12. // Fetch google page via local proxy.
  13. statusCode, body, err := c.Get(nil, "http://google.com/foo/bar")
  14. if err != nil {
  15. log.Fatalf("Error when loading google page through local proxy: %s", err)
  16. }
  17. if statusCode != fasthttp.StatusOK {
  18. log.Fatalf("Unexpected status code: %d. Expecting %d", statusCode, fasthttp.StatusOK)
  19. }
  20. useResponseBody(body)
  21. // Fetch foobar page via local proxy. Reuse body buffer.
  22. statusCode, body, err = c.Get(body, "http://foobar.com/google/com")
  23. if err != nil {
  24. log.Fatalf("Error when loading foobar page through local proxy: %s", err)
  25. }
  26. if statusCode != fasthttp.StatusOK {
  27. log.Fatalf("Unexpected status code: %d. Expecting %d", statusCode, fasthttp.StatusOK)
  28. }
  29. useResponseBody(body)
  30. }
  31. func useResponseBody(body []byte) {
  32. // Do something with body :)
  33. }