Source File
response.go
Belonging Package
github.com/go-resty/resty/v2
// Copyright (c) 2015-2023 Jeevanandam M (jeeva@myjeeva.com), All rights reserved.
// resty source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package resty
import (
)
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Response struct and methods
//_______________________________________________________________________
// Response struct holds response values of executed request.
type Response struct {
Request *Request
RawResponse *http.Response
body []byte
size int64
receivedAt time.Time
}
// Body method returns HTTP response as []byte array for the executed request.
//
// Note: `Response.Body` might be nil, if `Request.SetOutput` is used.
func ( *Response) () []byte {
if .RawResponse == nil {
return []byte{}
}
return .body
}
// SetBody method is to set Response body in byte slice. Typically,
// its helpful for test cases.
//
// resp.SetBody([]byte("This is test body content"))
// resp.SetBody(nil)
//
// Since v2.10.0
func ( *Response) ( []byte) *Response {
.body =
return
}
// Status method returns the HTTP status string for the executed request.
//
// Example: 200 OK
func ( *Response) () string {
if .RawResponse == nil {
return ""
}
return .RawResponse.Status
}
// StatusCode method returns the HTTP status code for the executed request.
//
// Example: 200
func ( *Response) () int {
if .RawResponse == nil {
return 0
}
return .RawResponse.StatusCode
}
// Proto method returns the HTTP response protocol used for the request.
func ( *Response) () string {
if .RawResponse == nil {
return ""
}
return .RawResponse.Proto
}
// Result method returns the response value as an object if it has one
func ( *Response) () interface{} {
return .Request.Result
}
// Error method returns the error object if it has one
func ( *Response) () interface{} {
return .Request.Error
}
// Header method returns the response headers
func ( *Response) () http.Header {
if .RawResponse == nil {
return http.Header{}
}
return .RawResponse.Header
}
// Cookies method to access all the response cookies
func ( *Response) () []*http.Cookie {
if .RawResponse == nil {
return make([]*http.Cookie, 0)
}
return .RawResponse.Cookies()
}
// String method returns the body of the server response as String.
func ( *Response) () string {
if len(.body) == 0 {
return ""
}
return strings.TrimSpace(string(.body))
}
// Time method returns the time of HTTP response time that from request we sent and received a request.
//
// See `Response.ReceivedAt` to know when client received response and see `Response.Request.Time` to know
// when client sent a request.
func ( *Response) () time.Duration {
if .Request.clientTrace != nil {
return .Request.TraceInfo().TotalTime
}
return .receivedAt.Sub(.Request.Time)
}
// ReceivedAt method returns when response got received from server for the request.
func ( *Response) () time.Time {
return .receivedAt
}
// Size method returns the HTTP response size in bytes. Ya, you can relay on HTTP `Content-Length` header,
// however it won't be good for chucked transfer/compressed response. Since Resty calculates response size
// at the client end. You will get actual size of the http response.
func ( *Response) () int64 {
return .size
}
// RawBody method exposes the HTTP raw response body. Use this method in-conjunction with `SetDoNotParseResponse`
// option otherwise you get an error as `read err: http: read on closed response body`.
//
// Do not forget to close the body, otherwise you might get into connection leaks, no connection reuse.
// Basically you have taken over the control of response parsing from `Resty`.
func ( *Response) () io.ReadCloser {
if .RawResponse == nil {
return nil
}
return .RawResponse.Body
}
// IsSuccess method returns true if HTTP status `code >= 200 and <= 299` otherwise false.
func ( *Response) () bool {
return .StatusCode() > 199 && .StatusCode() < 300
}
// IsError method returns true if HTTP status `code >= 400` otherwise false.
func ( *Response) () bool {
return .StatusCode() > 399
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Response Unexported methods
//_______________________________________________________________________
func ( *Response) () {
.receivedAt = time.Now()
if .Request.clientTrace != nil {
.Request.clientTrace.endTime = .receivedAt
}
}
func ( *Response) ( int64) string {
if len(.body) > 0 {
if int64(len(.body)) > {
return fmt.Sprintf("***** RESPONSE TOO LARGE (size - %d) *****", len(.body))
}
:= .Header().Get(hdrContentTypeKey)
if IsJSONType() {
:= acquireBuffer()
defer releaseBuffer()
:= json.Indent(, .body, "", " ")
if != nil {
return fmt.Sprintf("*** Error: Unable to format response body - \"%s\" ***\n\nLog Body as-is:\n%s", , .String())
}
return .String()
}
return .String()
}
return "***** NO CONTENT *****"
}
![]() |
The pages are generated with Golds v0.6.7. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |