📥 Response
The Response
structure in Gofiber's HTTP client represents the server's response to an HTTP request. It includes:
- 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.
This structure makes it easy to inspect and handle the data sent back by the server.
type Response struct {
client *Client
request *Request
cookie []*fasthttp.Cookie
RawResponse *fasthttp.Response
}
AcquireResponse
AcquireResponse returns a new (pooled) Response
object. When finished, release it using ReleaseResponse
to reduce GC overhead.
func AcquireResponse() *Response
ReleaseResponse
ReleaseResponse returns the Response
object to the pool. Avoid using the response after releasing it to prevent 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
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
Example with maps.Collect()
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
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 unmarshals the response body into the provided variable v
using JSON. v
should be a pointer to a struct or a type compatible with JSON unmarshalling.
func (r *Response) JSON(v any) error
Example
XML
XML unmarshals the response body into the provided variable v
using XML decoding.
func (r *Response) XML(v any) error
CBOR
CBOR unmarshals 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()