The Fiber HTTP client is a high-performance HTTP client built on top of fasthttp. It provides a clean, intuitive API for making HTTP requests while maintaining the speed and efficiency that Fiber is known for.
c := client.New()// Set base URLc.SetBaseURL("https://api.example.com")// Set timeoutc.SetTimeout(30 * time.Second)// Set default headersc.SetHeader("User-Agent", "MyApp/1.0")c.SetHeader("Accept", "application/json")// Set default query parametersc.SetParam("api_key", "your-api-key")
c := client.New()// Simple GET requestresp, err := c.Get("https://httpbin.org/get")// POST with body and headersresp, err := c.Post("https://httpbin.org/post", client.Config{ Body: map[string]string{"name": "John"}, Header: map[string]string{"Content-Type": "application/json"},})
The client uses object pooling to reduce allocations:
// Acquire a request from the poolreq := client.AcquireRequest()defer client.ReleaseRequest(req)// Set client and configure requestreq.SetClient(c). SetURL("https://httpbin.org/get"). SetMethod(fiber.MethodGet)// Send the requestresp, err := req.Send()if err != nil { panic(err)}defer resp.Close() // Automatically releases both request and response
Always call resp.Close() when using the client to return resources to the pool. Do not use requests or responses after releasing them.