@@ -28,12 +28,25 @@ import (
2828)
2929
3030const (
31- GET = "GET"
32- POST = "POST"
33- PUT = "PUT"
34- DELETE = "DELETE"
35- PATCH = "PATCH"
36- HEAD = "HEAD"
31+ // HTTP method GET
32+ GET = "GET"
33+
34+ // HTTP method POST
35+ POST = "POST"
36+
37+ // HTTP method PUT
38+ PUT = "PUT"
39+
40+ // HTTP method DELETE
41+ DELETE = "DELETE"
42+
43+ // HTTP method PATCH
44+ PATCH = "PATCH"
45+
46+ // HTTP method HEAD
47+ HEAD = "HEAD"
48+
49+ // HTTP method OPTIONS
3750 OPTIONS = "OPTIONS"
3851)
3952
5871// Client type is used for HTTP/RESTful global values
5972// for all request raised from the client
6073type Client struct {
61- HostUrl string
74+ HostURL string
6275 QueryParam url.Values
6376 FormData url.Values
6477 Header http.Header
@@ -77,15 +90,15 @@ type Client struct {
7790 afterResponse []func (* Client , * Response ) error
7891}
7992
80- // Type User is hold a username and password information
93+ // User type is to hold an username and password information
8194type User struct {
8295 Username , Password string
8396}
8497
8598// SetHostUrl method is to set Host URL in the client instance. It will be used with request
8699// raised from this client with relative URL
87- func (c * Client ) SetHostUrl (url string ) * Client {
88- c .HostUrl = strings .TrimRight (url , "/" )
100+ func (c * Client ) SetHostURL (url string ) * Client {
101+ c .HostURL = strings .TrimRight (url , "/" )
89102 return c
90103}
91104
@@ -133,7 +146,7 @@ func (c *Client) SetHeaders(headers map[string]string) *Client {
133146// Domain: "sample.com",
134147// MaxAge: 36000,
135148// HttpOnly: true,
136- // Secure: false, // baseds on https or http
149+ // Secure: false,
137150// })
138151//
139152func (c * Client ) SetCookie (hc * http.Cookie ) * Client {
@@ -254,7 +267,7 @@ func (c *Client) SetAuthToken(token string) *Client {
254267// R method creates a request instance, its used for Get, Post, Put, Delete, Patch, Head and Options.
255268func (c * Client ) R () * Request {
256269 r := & Request {
257- Url : "" ,
270+ URL : "" ,
258271 Method : "" ,
259272 QueryParam : url.Values {},
260273 FormData : url.Values {},
@@ -501,7 +514,7 @@ func (c *Client) disableLogPrefix() {
501514// and also you can add more options for that particular request
502515//
503516type Request struct {
504- Url string
517+ URL string
505518 Method string
506519 QueryParam url.Values
507520 FormData url.Values
@@ -786,7 +799,7 @@ func (r *Request) execute(method, url string) (*Response, error) {
786799 }
787800
788801 r .Method = method
789- r .Url = url
802+ r .URL = url
790803
791804 return r .client .execute (r )
792805}
@@ -900,21 +913,21 @@ func DetectContentType(body interface{}) string {
900913 return contentType
901914}
902915
903- // IsJsonType method is to check JSON content type or not
904- func IsJsonType (ct string ) bool {
916+ // IsJSONType method is to check JSON content type or not
917+ func IsJSONType (ct string ) bool {
905918 return jsonCheck .MatchString (ct )
906919}
907920
908- // IsXmlType method is to check XML content type or not
909- func IsXmlType (ct string ) bool {
921+ // IsXMLType method is to check XML content type or not
922+ func IsXMLType (ct string ) bool {
910923 return xmlCheck .MatchString (ct )
911924}
912925
913926// Unmarshal content into object from JSON or XML
914927func Unmarshal (ct string , b []byte , d interface {}) (err error ) {
915- if IsJsonType (ct ) {
928+ if IsJSONType (ct ) {
916929 err = json .Unmarshal (b , d )
917- } else if IsXmlType (ct ) {
930+ } else if IsXMLType (ct ) {
918931 err = xml .Unmarshal (b , d )
919932 }
920933
@@ -964,17 +977,20 @@ func getRequestBodyString(r *Request) (body string) {
964977 var prtBodyBytes []byte
965978 var err error
966979 kind := reflect .ValueOf (r .Body ).Kind ()
967- if IsJsonType (contentType ) && (kind == reflect .Struct || kind == reflect .Map ) {
980+ if IsJSONType (contentType ) && (kind == reflect .Struct || kind == reflect .Map ) {
968981 prtBodyBytes , err = json .MarshalIndent (& r .Body , "" , " " )
969- } else if IsXmlType (contentType ) && (kind == reflect .Struct ) {
982+ } else if IsXMLType (contentType ) && (kind == reflect .Struct ) {
970983 prtBodyBytes , err = xml .MarshalIndent (& r .Body , "" , " " )
971984 } else if b , ok := r .Body .(string ); ok {
972- if IsJsonType (contentType ) {
985+ if IsJSONType (contentType ) {
973986 bodyBytes := []byte (b )
974987 var out bytes.Buffer
975988 if err = json .Indent (& out , bodyBytes , "" , " " ); err == nil {
976989 prtBodyBytes = out .Bytes ()
977990 }
991+ } else {
992+ body = b
993+ return
978994 }
979995 } else if b , ok := r .Body .([]byte ); ok {
980996 body = base64 .StdEncoding .EncodeToString (b )
@@ -994,7 +1010,7 @@ func getResponseBodyString(res *Response) string {
9941010 bodyStr := "***** NO CONTENT *****"
9951011 if res .Body != nil {
9961012 ct := res .Header ().Get (hdrContentTypeKey )
997- if IsJsonType (ct ) {
1013+ if IsJSONType (ct ) {
9981014 var out bytes.Buffer
9991015 if err := json .Indent (& out , res .Body , "" , " " ); err == nil {
10001016 bodyStr = string (out .Bytes ())
0 commit comments