📥 Response
The Response struct in Fiber's HTTP client represents the server's reply and exposes:
- Status Code: The HTTP status code returned by the server (e.g.,
200 OK,404 Not Found). - Headers: All HTTP headers returned by the server, providing additional response-related information.
- Body: The response body content, which can be JSON, XML, plain text, or other formats.
- Cookies: Any cookies the server sent along with the response.
It makes it easy to inspect and handle data returned by the server.
type Response struct {
client *Client
request *Request
cookie []*fasthttp.Cookie
RawResponse *fasthttp.Response
}
AcquireResponse
AcquireResponse returns a new pooled Response. Call ReleaseResponse when you're done to return it to the pool and limit allocations.
func AcquireResponse() *Response
ReleaseResponse
ReleaseResponse puts the Response back into the pool. Do not use it after releasing; doing so can trigger data races.
func ReleaseResponse(resp *Response)
Status
Status returns the HTTP status message (e.g., OK, Not Found) associated with the response.
func (r *Response) Status() string
StatusCode
StatusCode returns the numeric HTTP status code of the response.
func (r *Response) StatusCode() int
Protocol
Protocol returns the HTTP protocol used (e.g., HTTP/1.1, HTTP/2) for the response.
func (r *Response) Protocol() string
Example
resp, err := client.Get("https://httpbin.org/get")
if err != nil {
panic(err)
}
fmt.Println(resp.Protocol())
Output:
HTTP/1.1
Header
Header retrieves the value of a specific response header by key. If multiple values exist for the same header, this returns the first one.
func (r *Response) Header(key string) string
Headers
Headers returns an iterator over all response headers. Use maps.Collect() to convert them into a map if desired. The returned values are only valid until the response is released, so make copies if needed.
func (r *Response) Headers() iter.Seq2[string, []string]
Example
resp, err := client.Get("https://httpbin.org/get")
if err != nil {
panic(err)
}
for key, values := range resp.Headers() {
fmt.Printf("%s => %s\n", key, strings.Join(values, ", "))
}
Output:
Date => Wed, 04 Dec 2024 15:28:29 GMT
Connection => keep-alive
Access-Control-Allow-Origin => *
Access-Control-Allow-Credentials => true
Example with maps.Collect()
resp, err := client.Get("https://httpbin.org/get")
if err != nil {
panic(err)
}
headers := maps.Collect(resp.Headers())
for key, values := range headers {
fmt.Printf("%s => %s\n", key, strings.Join(values, ", "))
}
Output:
Date => Wed, 04 Dec 2024 15:28:29 GMT
Connection => keep-alive
Access-Control-Allow-Origin => *
Access-Control-Allow-Credentials => true
Cookies
Cookies returns a slice of all cookies set by the server in this response. The slice is only valid until the response is released.
func (r *Response) Cookies() []*fasthttp.Cookie
Example
resp, err := client.Get("https://httpbin.org/cookies/set/go/fiber")
if err != nil {
panic(err)
}
cookies := resp.Cookies()
for _, cookie := range cookies {
fmt.Printf("%s => %s\n", string(cookie.Key()), string(cookie.Value()))
}
Output:
go => fiber
Body
Body returns the raw response body as a byte slice.
func (r *Response) Body() []byte
String
String returns the response body as a trimmed string.
func (r *Response) String() string
JSON
JSON unmarshal the response body into the provided variable v using JSON. v should be a pointer to a struct or a type compatible with JSON unmarshal.
func (r *Response) JSON(v any) error
Example
type Body struct {
Slideshow struct {
Author string `json:"author"`
Date string `json:"date"`
Title string `json:"title"`
} `json:"slideshow"`
}
var out Body
resp, err := client.Get("https://httpbin.org/json")
if err != nil {
panic(err)
}
if err = resp.JSON(&out); err != nil {
panic(err)
}
fmt.Printf("%+v\n", out)
Output:
{Slideshow:{Author:Yours Truly Date:date of publication Title:Sample Slide Show}}
XML
XML unmarshal the response body into the provided variable v using XML decoding.
func (r *Response) XML(v any) error
CBOR
CBOR unmarshal the response body into v using CBOR decoding.
func (r *Response) CBOR(v any) error
Save
Save writes the response body to a file or an io.Writer. If v is a string, it interprets it as a file path, creates the file (and directories if needed), and writes the response to it. If v is an io.Writer, it writes directly to it.
func (r *Response) Save(v any) error
Reset
Reset clears the Response object, making it ready for reuse by ReleaseResponse.
func (r *Response) Reset()
Close
Close releases both the associated Request and Response objects back to their pools.
After calling Close, any attempt to use the request or response may result in data races or undefined behavior. Ensure all processing is complete before closing.
func (r *Response) Close()