🌎
Client
The Client struct represents the Fiber HTTP Client.

Start request

Start a http request with http method and url.
Signatures
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
Copied!

✨ 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.
Parse
1
a := AcquireAgent()
2
req := a.Request()
3
req.Header.SetMethod(MethodGet)
4
req.SetRequestURI("http://example.com")
5
​
6
if err := a.Parse(); err != nil {
7
panic(err)
8
}
9
​
10
code, body, errs := a.Bytes() // ...
Copied!

Set

Set sets the given key: value header.
Signature
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
Copied!
Example
1
agent.Set("k1", "v1").
2
SetBytesK([]byte("k1"), "v1").
3
SetBytesV("k1", []byte("v1")).
4
SetBytesKV([]byte("k2"), []byte("v2"))
5
// ...
Copied!

Add

Add adds the given key: value header. Multiple headers with the same key may be added with this function.
Signature
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
Copied!
Example
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
Copied!

ConnectionClose

ConnectionClose adds the Connection: close header.
Signature
1
func (a *Agent) ConnectionClose() *Agent
Copied!
Example
1
agent.ConnectionClose()
2
// ...
Copied!

UserAgent

UserAgent sets User-Agent header value.
Signature
1
func (a *Agent) UserAgent(userAgent string) *Agent
2
func (a *Agent) UserAgentBytes(userAgent []byte) *Agent
Copied!
Example
1
agent.UserAgent("fiber")
2
// ...
Copied!
Cookie sets a cookie in key: value form. Cookies can be used to set multiple cookies.
Signature
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
Copied!
Example
1
agent.Cookie("k", "v")
2
agent.Cookies("k1", "v1", "k2", "v2")
3
// ...
Copied!

Referer

Referer sets the Referer header value.
Signature
1
func (a *Agent) Referer(referer string) *Agent
2
func (a *Agent) RefererBytes(referer []byte) *Agent
Copied!
Example
1
agent.Referer("https://docs.gofiber.io")
2
// ...
Copied!

ContentType

ContentType sets Content-Type header value.
Signature
1
func (a *Agent) ContentType(contentType string) *Agent
2
func (a *Agent) ContentTypeBytes(contentType []byte) *Agent
Copied!
Example
1
agent.ContentType("custom-type")
2
// ...
Copied!

Host

Host sets the Host header.
Signature
1
func (a *Agent) Host(host string) *Agent
2
func (a *Agent) HostBytes(host []byte) *Agent
Copied!
Example
1
agent.Host("example.com")
2
// ...
Copied!

QueryString

QueryString sets the URI query string.
Signature
1
func (a *Agent) QueryString(queryString string) *Agent
2
func (a *Agent) QueryStringBytes(queryString []byte) *Agent
Copied!
Example
1
agent.QueryString("foo=bar")
2
// ...
Copied!

BasicAuth

BasicAuth sets the URI username and password using HTTP Basic Auth.
Signature
1
func (a *Agent) BasicAuth(username, password string) *Agent
2
func (a *Agent) BasicAuthBytes(username, password []byte) *Agent
Copied!
Example
1
agent.BasicAuth("foo", "bar")
2
// ...
Copied!

Body

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

JSON

JSON sends a JSON request by setting the Content-Type header to application/json.
Signature
1
func (a *Agent) JSON(v interface{}) *Agent
Copied!
Example
1
agent.JSON(fiber.Map{"success": true})
2
// ...
Copied!

XML

XML sends an XML request by setting the Content-Type header to application/xml.
Signature
1
func (a *Agent) XML(v interface{}) *Agent
Copied!
Example
1
agent.XML(fiber.Map{"success": true})
2
// ...
Copied!

Form

Form sends a form request by setting the Content-Type header to application/x-www-form-urlencoded.
Signature
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
Copied!
Example
1
args := AcquireArgs()
2
args.Set("foo", "bar")
3
​
4
agent.Form(args)
5
// ...
6
ReleaseArgs(args)
Copied!

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.
Signature
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
Copied!
Example
1
args := AcquireArgs()
2
args.Set("foo", "bar")
3
​
4
agent.MultipartForm(args)
5
// ...
6
ReleaseArgs(args)
Copied!
Fiber provides several methods for sending files. Note that they must be called before MultipartForm.

Boundary

Boundary sets boundary for multipart form request.
Signature
1
func (a *Agent) Boundary(boundary string) *Agent
Copied!
Example
1
agent.Boundary("myBoundary")
2
.MultipartForm(nil)
3
// ...
Copied!

SendFile(s)

SendFile read a file and appends it to a multipart form request. Sendfiles can be used to append multiple files.
Signature
1
func (a *Agent) SendFile(filename string, fieldname ...string) *Agent
2
func (a *Agent) SendFiles(filenamesAndFieldnames ...string) *Agent
Copied!
Example
1
agent.SendFile("f", "field name")
2
.SendFiles("f1", "field name1", "f2").
3
.MultipartForm(nil)
4
// ...
Copied!

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
}
Copied!
Signature
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
Copied!
Example
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
// ...
Copied!

Debug

Debug mode enables logging request and response detail to io.writer(default is os.Stdout).
Signature
1
func (a *Agent) Debug(w ...io.Writer) *Agent
Copied!
Example
1
agent.Debug()
2
// ...
Copied!

Timeout

Timeout sets request timeout duration.
Signature
1
func (a *Agent) Timeout(timeout time.Duration) *Agent
Copied!
Example
1
agent.Timeout(time.Second)
2
// ...
Copied!

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.
Signature
1
func (a *Agent) Reuse() *Agent
Copied!
Example
1
agent.Reuse()
2
// ...
Copied!

InsecureSkipVerify

InsecureSkipVerify controls whether the Agent verifies the server certificate chain and host name.
Signature
1
func (a *Agent) InsecureSkipVerify() *Agent
Copied!
Example
1
agent.InsecureSkipVerify()
2
// ...
Copied!

TLSConfig

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

MaxRedirectsCount

MaxRedirectsCount sets max redirect count for GET and HEAD.
Signature
1
func (a *Agent) MaxRedirectsCount(count int) *Agent
Copied!
Example
1
agent.MaxRedirectsCount(7)
2
// ...
Copied!

JSONEncoder

JSONEncoder sets custom json encoder.
Signature
1
func (a *Agent) JSONEncoder(jsonEncoder utils.JSONMarshal) *Agent
Copied!
Example
1
agent.JSONEncoder(json.Marshal)
2
// ...
Copied!

JSONDecoder

JSONDecoder sets custom json decoder.
Signature
1
func (a *Agent) JSONDecoder(jsonDecoder utils.JSONUnmarshal) *Agent
Copied!
Example
1
agent.JSONDecoder(json.Unmarshal)
2
// ...
Copied!

Request

Request returns Agent request instance.
Signature
1
func (a *Agent) Request() *Request
Copied!
Example
1
req := agent.Request()
2
// ...
Copied!

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.
Signature
1
func (a *Agent) SetResponse(customResp *Response) *Agent
Copied!
Example
1
resp := AcquireResponse()
2
agent.SetResponse(resp)
3
// ...
4
ReleaseResponse(resp)
Copied!

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.
Signature
1
func (a *Agent) Dest(dest []byte) *Agent {
Copied!
Example
1
agent.Dest(nil)
2
// ...
Copied!

Bytes

Bytes returns the status code, bytes body and errors of url.
Signature
1
func (a *Agent) Bytes() (code int, body []byte, errs []error)
Copied!
Example
1
code, body, errs := agent.Bytes()
2
// ...
Copied!

String

String returns the status code, string body and errors of url.
Signature
1
func (a *Agent) String() (int, string, []error)
Copied!
Example
1
code, body, errs := agent.String()
2
// ...
Copied!

Struct

Struct returns the status code, bytes body and errors of url. And bytes body will be unmarshalled to given v.
Signature
1
func (a *Agent) Struct(v interface{}) (code int, body []byte, errs []error)
Copied!
Example
1
var d data
2
code, body, errs := agent.Struct(&d)
3
// ...
Copied!
Last modified 6mo ago