Result
Result wraps HTTP response and request metadata, providing convenient access methods. Obtained via Client.Request() or package-level functions.
type Result struct {
Request *RequestInfo
Response *ResponseInfo
Meta *RequestMeta
}result, err := httpc.Get("https://api.example.com/users/1")
if err != nil {
log.Fatal(err)
}
fmt.Println(result.StatusCode()) // 200
fmt.Println(result.Body()) // {"id":1,"name":"test"}TIP
Result is created fresh for each request and automatically reclaimed by GC -- no manual release needed.
Basic Methods
StatusCode
func (r *Result) StatusCode() intReturns the HTTP status code. Nil-safe, returns 0.
Body
func (r *Result) Body() stringReturns the response body as a string. Nil-safe, returns empty string.
RawBody
func (r *Result) RawBody() []byteReturns the response body as raw bytes. Nil-safe, returns nil.
Proto
func (r *Result) Proto() stringReturns the HTTP protocol version, e.g. "HTTP/1.1", "HTTP/2.0".
Status Checks
IsSuccess
func (r *Result) IsSuccess() boolReturns true for 2xx status codes.
IsRedirect
func (r *Result) IsRedirect() boolReturns true for 3xx status codes.
IsClientError
func (r *Result) IsClientError() boolReturns true for 4xx status codes.
IsServerError
func (r *Result) IsServerError() boolReturns true for 5xx status codes.
result, _ := client.Get(url)
switch {
case result.IsSuccess():
handleSuccess(result)
case result.IsClientError():
handleClientError(result)
case result.IsServerError():
handleServerError(result)
}Cookie Methods
ResponseCookies
func (r *Result) ResponseCookies() []*http.CookieReturns all cookies from the response.
GetCookie
func (r *Result) GetCookie(name string) *http.CookieGets a response cookie by name, returns nil if not found.
cookie := result.GetCookie("session")
if cookie != nil {
fmt.Println(cookie.Value)
}HasCookie
func (r *Result) HasCookie(name string) boolChecks whether a cookie with the specified name exists in the response.
RequestCookies
func (r *Result) RequestCookies() []*http.CookieReturns all cookies sent with the request.
GetRequestCookie
func (r *Result) GetRequestCookie(name string) *http.CookieGets a request cookie by name.
HasRequestCookie
func (r *Result) HasRequestCookie(name string) boolChecks whether a cookie with the specified name exists in the request.
JSON Parsing
Unmarshal
func (r *Result) Unmarshal(v any) errorParses the JSON response body into the target variable. Follows json.Unmarshal conventions.
| Error | Trigger Condition |
|---|---|
ErrResponseBodyEmpty | Response body is empty |
ErrResponseBodyTooLarge | Response body exceeds 50MB JSON parsing size limit |
var user User
if err := result.Unmarshal(&user); err != nil {
log.Fatal(err)
}
fmt.Println(user.Name)File Saving
SaveToFile
func (r *Result) SaveToFile(filePath string) errorSaves the response body to a file. File path is security-validated (path traversal protection, symlink checking, system path protection).
| Error | Trigger Condition |
|---|---|
ErrResponseBodyEmpty | Response body is empty |
result, _ := client.Get("https://example.com/data.csv")
if err := result.SaveToFile("/tmp/data.csv"); err != nil {
log.Fatal(err)
}String Representation
String
func (r *Result) String() stringReturns a human-readable string representation. Sensitive headers are automatically masked, response body is truncated to 200 characters.
result, _ := client.Get(url)
fmt.Println(result.String())
// Result{Status: 200 200 OK, ContentLength: 1024, Duration: 125ms, Attempts: 1, ...}Sub-types
RequestInfo
type RequestInfo struct {
URL string
Method string
Headers http.Header
Cookies []*http.Cookie
}Request details. Access via result.Request.
ResponseInfo
type ResponseInfo struct {
StatusCode int
Status string
Proto string
Headers http.Header
Body string
RawBody []byte
ContentLength int64
Cookies []*http.Cookie
}Response data. Access via result.Response.
RequestMeta
type RequestMeta struct {
Duration time.Duration
Attempts int
RedirectChain []string
RedirectCount int
}Request execution metadata. Access via result.Meta.
result, _ := client.Get(url)
fmt.Println(result.Meta.Duration) // 125ms
fmt.Println(result.Meta.Attempts) // 2 (retried once)
fmt.Println(result.Meta.RedirectCount) // 1 (followed one redirect)See Also
- Package Functions - Request methods that return Result
- Request Options - Configure request behavior
- File Download - Download result type DownloadResult