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
|
package main
import (
"fmt"
"os"
"github.com/valyala/fasthttp"
)
func main() {
// Get URI from a pool
url := fasthttp.AcquireURI()
url.Parse(nil, []byte("http://localhost:8080/"))
url.SetUsername("Aladdin")
url.SetPassword("Open Sesame")
hc := &fasthttp.HostClient{
Addr: "localhost:8080", // The host address and port must be set explicitly
}
req := fasthttp.AcquireRequest()
req.SetURI(url) // copy url into request
fasthttp.ReleaseURI(url) // now you may release the URI
req.Header.SetMethod(fasthttp.MethodGet)
resp := fasthttp.AcquireResponse()
err := hc.Do(req, resp)
fasthttp.ReleaseRequest(req)
if err == nil {
fmt.Printf("Response: %s\n", resp.Body())
} else {
fmt.Fprintf(os.Stderr, "Connection error: %v\n", err)
}
fasthttp.ReleaseResponse(resp)
}
|