description: The Client struct represents the Fiber HTTP Client.

🌎 Client

Start request

Start a http request with http method and url.

  1. // Client http methods
  2. func (c *Client) Get(url string) *Agent
  3. func (c *Client) Head(url string) *Agent
  4. func (c *Client) Post(url string) *Agent
  5. func (c *Client) Put(url string) *Agent
  6. func (c *Client) Patch(url string) *Agent
  7. func (c *Client) Delete(url string) *Agent

✨ Agent

Agent is built on top of FastHTTP’s HostClient which has lots of convenient helper methods such as dedicated methods for request methods.

Parse

Parse initializes a HostClient.

  1. a := AcquireAgent()
  2. req := a.Request()
  3. req.Header.SetMethod(MethodGet)
  4. req.SetRequestURI("http://example.com")
  5. if err := a.Parse(); err != nil {
  6. panic(err)
  7. }
  8. code, body, errs := a.Bytes() // ...

Set

Set sets the given key: value header.

  1. func (a *Agent) Set(k, v string) *Agent
  2. func (a *Agent) SetBytesK(k []byte, v string) *Agent
  3. func (a *Agent) SetBytesV(k string, v []byte) *Agent
  4. func (a *Agent) SetBytesKV(k []byte, v []byte) *Agent
  1. agent.Set("k1", "v1").
  2. SetBytesK([]byte("k1"), "v1").
  3. SetBytesV("k1", []byte("v1")).
  4. SetBytesKV([]byte("k2"), []byte("v2"))
  5. // ...

Add

Add adds the given key: value header. Multiple headers with the same key may be added with this function.

  1. func (a *Agent) Add(k, v string) *Agent
  2. func (a *Agent) AddBytesK(k []byte, v string) *Agent
  3. func (a *Agent) AddBytesV(k string, v []byte) *Agent
  4. func (a *Agent) AddBytesKV(k []byte, v []byte) *Agent
  1. agent.Add("k1", "v1").
  2. AddBytesK([]byte("k1"), "v1").
  3. AddBytesV("k1", []byte("v1")).
  4. AddBytesKV([]byte("k2"), []byte("v2"))
  5. // Headers:
  6. // K1: v1
  7. // K1: v1
  8. // K1: v1
  9. // K2: v2

ConnectionClose

ConnectionClose adds the Connection: close header.

  1. func (a *Agent) ConnectionClose() *Agent
  1. agent.ConnectionClose()
  2. // ...

UserAgent

UserAgent sets User-Agent header value.

  1. func (a *Agent) UserAgent(userAgent string) *Agent
  2. func (a *Agent) UserAgentBytes(userAgent []byte) *Agent
  1. agent.UserAgent("fiber")
  2. // ...

Cookie

Cookie sets a cookie in key: value form. Cookies can be used to set multiple cookies.

  1. func (a *Agent) Cookie(key, value string) *Agent
  2. func (a *Agent) CookieBytesK(key []byte, value string) *Agent
  3. func (a *Agent) CookieBytesKV(key, value []byte) *Agent
  4. func (a *Agent) Cookies(kv ...string) *Agent
  5. func (a *Agent) CookiesBytesKV(kv ...[]byte) *Agent
  1. agent.Cookie("k", "v")
  2. agent.Cookies("k1", "v1", "k2", "v2")
  3. // ...

Referer

Referer sets the Referer header value.

  1. func (a *Agent) Referer(referer string) *Agent
  2. func (a *Agent) RefererBytes(referer []byte) *Agent
  1. agent.Referer("https://docs.gofiber.io")
  2. // ...

ContentType

ContentType sets Content-Type header value.

  1. func (a *Agent) ContentType(contentType string) *Agent
  2. func (a *Agent) ContentTypeBytes(contentType []byte) *Agent
  1. agent.ContentType("custom-type")
  2. // ...

Host

Host sets the Host header.

  1. func (a *Agent) Host(host string) *Agent
  2. func (a *Agent) HostBytes(host []byte) *Agent
  1. agent.Host("example.com")
  2. // ...

QueryString

QueryString sets the URI query string.

  1. func (a *Agent) QueryString(queryString string) *Agent
  2. func (a *Agent) QueryStringBytes(queryString []byte) *Agent
  1. agent.QueryString("foo=bar")
  2. // ...

BasicAuth

BasicAuth sets the URI username and password using HTTP Basic Auth.

  1. func (a *Agent) BasicAuth(username, password string) *Agent
  2. func (a *Agent) BasicAuthBytes(username, password []byte) *Agent
  1. agent.BasicAuth("foo", "bar")
  2. // ...

Body

There are several ways to set request body.

  1. func (a *Agent) BodyString(bodyString string) *Agent
  2. func (a *Agent) Body(body []byte) *Agent
  3. // BodyStream sets request body stream and, optionally body size.
  4. //
  5. // If bodySize is >= 0, then the bodyStream must provide exactly bodySize bytes
  6. // before returning io.EOF.
  7. //
  8. // If bodySize < 0, then bodyStream is read until io.EOF.
  9. //
  10. // bodyStream.Close() is called after finishing reading all body data
  11. // if it implements io.Closer.
  12. //
  13. // Note that GET and HEAD requests cannot have body.
  14. func (a *Agent) BodyStream(bodyStream io.Reader, bodySize int) *Agent
  1. agent.BodyString("foo=bar")
  2. agent.Body([]byte("bar=baz"))
  3. agent.BodyStream(strings.NewReader("body=stream"), -1)
  4. // ...

JSON

JSON sends a JSON request by setting the Content-Type header to application/json.

  1. func (a *Agent) JSON(v interface{}) *Agent
  1. agent.JSON(fiber.Map{"success": true})
  2. // ...

XML

XML sends an XML request by setting the Content-Type header to application/xml.

  1. func (a *Agent) XML(v interface{}) *Agent
  1. agent.XML(fiber.Map{"success": true})
  2. // ...

Form

Form sends a form request by setting the Content-Type header to application/x-www-form-urlencoded.

  1. // Form sends form request with body if args is non-nil.
  2. //
  3. // It is recommended obtaining args via AcquireArgs and release it
  4. // manually in performance-critical code.
  5. func (a *Agent) Form(args *Args) *Agent
  1. args := AcquireArgs()
  2. args.Set("foo", "bar")
  3. agent.Form(args)
  4. // ...
  5. ReleaseArgs(args)

MultipartForm

MultipartForm sends multipart form request by setting the Content-Type header to multipart/form-data. These requests can include key-value’s and files.

  1. // MultipartForm sends multipart form request with k-v and files.
  2. //
  3. // It is recommended to obtain args via AcquireArgs and release it
  4. // manually in performance-critical code.
  5. func (a *Agent) MultipartForm(args *Args) *Agent
  1. args := AcquireArgs()
  2. args.Set("foo", "bar")
  3. agent.MultipartForm(args)
  4. // ...
  5. ReleaseArgs(args)

Fiber provides several methods for sending files. Note that they must be called before MultipartForm.

Boundary

Boundary sets boundary for multipart form request.

  1. func (a *Agent) Boundary(boundary string) *Agent
  1. agent.Boundary("myBoundary")
  2. .MultipartForm(nil)
  3. // ...

SendFile(s)

SendFile read a file and appends it to a multipart form request. Sendfiles can be used to append multiple files.

  1. func (a *Agent) SendFile(filename string, fieldname ...string) *Agent
  2. func (a *Agent) SendFiles(filenamesAndFieldnames ...string) *Agent
  1. agent.SendFile("f", "field name")
  2. .SendFiles("f1", "field name1", "f2").
  3. .MultipartForm(nil)
  4. // ...

FileData

FileData appends file data for multipart form request.

  1. // FormFile represents multipart form file
  2. type FormFile struct {
  3. // Fieldname is form file's field name
  4. Fieldname string
  5. // Name is form file's name
  6. Name string
  7. // Content is form file's content
  8. Content []byte
  9. }
  1. // FileData appends files for multipart form request.
  2. //
  3. // It is recommended obtaining formFile via AcquireFormFile and release it
  4. // manually in performance-critical code.
  5. func (a *Agent) FileData(formFiles ...*FormFile) *Agent
  1. ff1 := &FormFile{"filename1", "field name1", []byte("content")}
  2. ff2 := &FormFile{"filename2", "field name2", []byte("content")}
  3. agent.FileData(ff1, ff2).
  4. MultipartForm(nil)
  5. // ...

Debug

Debug mode enables logging request and response detail to io.writer(default is os.Stdout).

  1. func (a *Agent) Debug(w ...io.Writer) *Agent
  1. agent.Debug()
  2. // ...

Timeout

Timeout sets request timeout duration.

  1. func (a *Agent) Timeout(timeout time.Duration) *Agent
  1. agent.Timeout(time.Second)
  2. // ...

Reuse

Reuse enables the Agent instance to be used again after one request. If agent is reusable, then it should be released manually when it is no longer used.

  1. func (a *Agent) Reuse() *Agent
  1. agent.Reuse()
  2. // ...

InsecureSkipVerify

InsecureSkipVerify controls whether the Agent verifies the server certificate chain and host name.

  1. func (a *Agent) InsecureSkipVerify() *Agent
  1. agent.InsecureSkipVerify()
  2. // ...

TLSConfig

TLSConfig sets tls config.

  1. func (a *Agent) TLSConfig(config *tls.Config) *Agent
  1. // Create tls certificate
  2. cer, _ := tls.LoadX509KeyPair("pem", "key")
  3. config := &tls.Config{
  4. Certificates: []tls.Certificate{cer},
  5. }
  6. agent.TLSConfig(config)
  7. // ...

MaxRedirectsCount

MaxRedirectsCount sets max redirect count for GET and HEAD.

  1. func (a *Agent) MaxRedirectsCount(count int) *Agent
  1. agent.MaxRedirectsCount(7)
  2. // ...

JSONEncoder

JSONEncoder sets custom json encoder.

  1. func (a *Agent) JSONEncoder(jsonEncoder utils.JSONMarshal) *Agent
  1. agent.JSONEncoder(json.Marshal)
  2. // ...

JSONDecoder

JSONDecoder sets custom json decoder.

  1. func (a *Agent) JSONDecoder(jsonDecoder utils.JSONUnmarshal) *Agent
  1. agent.JSONDecoder(json.Unmarshal)
  2. // ...

Request

Request returns Agent request instance.

  1. func (a *Agent) Request() *Request
  1. req := agent.Request()
  2. // ...

SetResponse

SetResponse sets custom response for the Agent instance. It is recommended obtaining custom response via AcquireResponse and release it manually in performance-critical code.

  1. func (a *Agent) SetResponse(customResp *Response) *Agent
  1. resp := AcquireResponse()
  2. agent.SetResponse(resp)
  3. // ...
  4. ReleaseResponse(resp)

Dest

Dest sets custom dest. The contents of dest will be replaced by the response body, if the dest is too small a new slice will be allocated.

  1. func (a *Agent) Dest(dest []byte) *Agent {
  1. agent.Dest(nil)
  2. // ...

Bytes

Bytes returns the status code, bytes body and errors of url.

  1. func (a *Agent) Bytes() (code int, body []byte, errs []error)
  1. code, body, errs := agent.Bytes()
  2. // ...

String

String returns the status code, string body and errors of url.

  1. func (a *Agent) String() (int, string, []error)
  1. code, body, errs := agent.String()
  2. // ...

Struct

Struct returns the status code, bytes body and errors of url. And bytes body will be unmarshalled to given v.

  1. func (a *Agent) Struct(v interface{}) (code int, body []byte, errs []error)
  1. var d data
  2. code, body, errs := agent.Struct(&d)
  3. // ...