fiber/docs/client/response.md

4.2 KiB

id title description sidebar_position
response 📥 Response Response methods of Gofiber HTTP client. 3

The Response structure in Gofiber's HTTP client represents the server's response to an HTTP request. It contains all the necessary information received from the server. This includes:

  • Status Code: The HTTP status code returned by the server (e.g., 200 OK, 404 Not Found).
  • Headers: HTTP headers received from the server that provide additional information about the response.
  • Body: The data received from the server, typically in the form of a JSON, XML, or plain text format.
  • Cookies: Any cookies sent by the server along with the response.

This structure allows users to easily access and manage the data returned by the server, facilitating efficient handling of HTTP responses.

type Response struct {
    client  *Client
    request *Request
    cookie  []*fasthttp.Cookie

    RawResponse *fasthttp.Response
}

AcquireResponse

AcquireResponse returns an empty response object from the pool. The returned response may be returned to the pool with ReleaseResponse when no longer needed. This allows reducing GC load.

func AcquireResponse() *Response

ReleaseResponse

ReleaseResponse returns the object acquired via AcquireResponse to the pool. Do not access the released Response object; otherwise, data races may occur.

func ReleaseResponse(resp *Response)

Status

Status method returns the HTTP status string for the executed request.

func (r *Response) Status() string

StatusCode

StatusCode method returns the HTTP status code for the executed request.

func (r *Response) StatusCode() int

Protocol

Protocol method returns the HTTP response protocol used for the request.

func (r *Response) Protocol() string
resp, err := client.Get("https://httpbin.org/get")
if err != nil {
    panic(err)
}

fmt.Println(resp.Protocol())
Click here to see the result
HTTP/1.1

Header

Header method returns the response headers.

func (r *Response) Header(key string) string

Cookies

Cookies method to access all the response cookies.

func (r *Response) Cookies() []*fasthttp.Cookie
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()))
}
Click here to see the result
go => fiber

Body

Body method returns HTTP response as []byte array for the executed request.

func (r *Response) Body() []byte

String

String method returns the body of the server response as String.

func (r *Response) String() string

JSON

JSON method will unmarshal body to json.

func (r *Response) JSON(v any) error
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)
}

err = resp.JSON(&out)
if err != nil {
    panic(err)
}

fmt.Printf("%+v\n", out)
Click here to see the result
{Slideshow:{Author:Yours Truly Date:date of publication Title:Sample Slide Show}}

XML

XML method will unmarshal body to xml.

func (r *Response) XML(v any) error

Save

Save method will save the body to a file or io.Writer.

func (r *Response) Save(v any) error

Reset

Reset clears the Response object.

func (r *Response) Reset() 

Close

Close method will release the Request and Response objects; after calling Close, please do not use these objects.

func (r *Response) Close()