Skip to content

Conversation

@viennadd
Copy link
Contributor

To solve #281

(c *Client) SetDebugBodyLimit(sl int64) of resty client works only on response logger,
but not request logger, resty will log the whole file content in strings when post files

This PR is trying to apply SetDebugBodyLimit() to request logger just like response logger.
@jeevatkm

@codecov
Copy link

codecov bot commented Jan 12, 2020

Codecov Report

Merging #297 into master will increase coverage by 0.03%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #297      +/-   ##
==========================================
+ Coverage    95.5%   95.54%   +0.03%     
==========================================
  Files           9        9              
  Lines        1157     1166       +9     
==========================================
+ Hits         1105     1114       +9     
  Misses         29       29              
  Partials       23       23
Impacted Files Coverage Δ
client.go 97.4% <ø> (ø) ⬆️
request.go 100% <100%> (ø) ⬆️
middleware.go 91.15% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3eb938f...3b17108. Read the comment docs.

Copy link
Member

@jeevatkm jeevatkm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@viennadd Thank you for the PR, appreciated 👍

I had a look at the changes, it seems func (r *Request) fmtBodyString(sl int64) (body string) needs changes/improvement.

@jeevatkm
Copy link
Member

@viennadd I have created the following patch based on your fork. Kindly apply it to the PR.

diff --git a/request.go b/request.go
index 5fe9cea..32481ea 100644
--- a/request.go
+++ b/request.go
@@ -7,7 +7,6 @@ package resty
 import (
 	"bytes"
 	"context"
-	"encoding/base64"
 	"encoding/json"
 	"encoding/xml"
 	"fmt"
@@ -664,66 +663,66 @@ type SRVRecord struct {
 
 func (r *Request) fmtBodyString(sl int64) (body string) {
 	body = "***** NO CONTENT *****"
-	if isPayloadSupported(r.Method, r.client.AllowGetMethodPayload) {
-		if _, ok := r.Body.(io.Reader); ok {
-			body = "***** BODY IS io.Reader *****"
-			return
-		}
+	if !isPayloadSupported(r.Method, r.client.AllowGetMethodPayload) {
+		return
+	}
 
-		// multipart or form-data
-		if r.isMultiPart || r.isFormData {
-			bodySize := int64(r.bodyBuf.Len())
-			if bodySize > sl {
-				body = fmt.Sprintf("***** REQUEST TOO LARGE (size - %d) *****", bodySize)
-				return
-			}
-			body = r.bodyBuf.String()
-			return
-		}
+	if _, ok := r.Body.(io.Reader); ok {
+		body = "***** BODY IS io.Reader *****"
+		return
+	}
 
-		// request body data
-		if r.Body == nil {
+	// multipart or form-data
+	if r.isMultiPart || r.isFormData {
+		bodySize := int64(r.bodyBuf.Len())
+		if bodySize > sl {
+			body = fmt.Sprintf("***** REQUEST TOO LARGE (size - %d) *****", bodySize)
 			return
 		}
-		var prtBodyBytes []byte
-		var err error
-
-		contentType := r.Header.Get(hdrContentTypeKey)
-		kind := kindOf(r.Body)
-		if canJSONMarshal(contentType, kind) {
-			prtBodyBytes, err = json.MarshalIndent(&r.Body, "", "   ")
-		} else if IsXMLType(contentType) && (kind == reflect.Struct) {
-			prtBodyBytes, err = xml.MarshalIndent(&r.Body, "", "   ")
-		} else if b, ok := r.Body.(string); ok {
-			bodySize := int64(len([]byte(b)))
-			if bodySize > sl {
-				body = fmt.Sprintf("***** REQUEST TOO LARGE (size - %d) *****", bodySize)
-				return
-			}
-			if IsJSONType(contentType) {
-				bodyBytes := []byte(b)
-				out := acquireBuffer()
-				defer releaseBuffer(out)
-				if err = json.Indent(out, bodyBytes, "", "   "); err == nil {
-					prtBodyBytes = out.Bytes()
-				}
-			} else {
-				body = b
-				return
+		body = r.bodyBuf.String()
+		return
+	}
+
+	// request body data
+	if r.Body == nil {
+		return
+	}
+	var prtBodyBytes []byte
+	var err error
+
+	contentType := r.Header.Get(hdrContentTypeKey)
+	kind := kindOf(r.Body)
+	if canJSONMarshal(contentType, kind) {
+		prtBodyBytes, err = json.MarshalIndent(&r.Body, "", "   ")
+	} else if IsXMLType(contentType) && (kind == reflect.Struct) {
+		prtBodyBytes, err = xml.MarshalIndent(&r.Body, "", "   ")
+	} else if b, ok := r.Body.(string); ok {
+		if IsJSONType(contentType) {
+			bodyBytes := []byte(b)
+			out := acquireBuffer()
+			defer releaseBuffer(out)
+			if err = json.Indent(out, bodyBytes, "", "   "); err == nil {
+				prtBodyBytes = out.Bytes()
 			}
-		} else if b, ok := r.Body.([]byte); ok {
-			body = base64.StdEncoding.EncodeToString(b)
+		} else {
+			body = b
 		}
+	} else if b, ok := r.Body.([]byte); ok {
+		body = fmt.Sprintf("***** BODY IS byte(s) (size - %d) *****", len(b))
+		return
+	}
 
-		if prtBodyBytes != nil && err == nil {
-			body = string(prtBodyBytes)
-		}
+	if prtBodyBytes != nil && err == nil {
+		body = string(prtBodyBytes)
 	}
 
-	bodySize := int64(len([]byte(body)))
-	if bodySize > sl {
-		body = fmt.Sprintf("***** REQUEST TOO LARGE (size - %d) *****", bodySize)
+	if len(body) > 0 {
+		bodySize := int64(len([]byte(body)))
+		if bodySize > sl {
+			body = fmt.Sprintf("***** REQUEST TOO LARGE (size - %d) *****", bodySize)
+		}
 	}
+
 	return
 }

@viennadd
Copy link
Contributor Author

viennadd commented Jan 13, 2020

@jeevatkm
I just have applied your improvements for func (r *Request) fmtBodyString(sl int64) (body string).
Thank you 👍

Copy link
Member

@jeevatkm jeevatkm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@viennadd Thank you 😄

@jeevatkm jeevatkm merged commit 57ba3ba into go-resty:master Jan 15, 2020
@jeevatkm jeevatkm added this to the v2.2.0 Milestone milestone Jan 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

2 participants