-
-
Notifications
You must be signed in to change notification settings - Fork 770
Apply debugBodySizeLimit to Request debug logger #297
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
jeevatkm
left a comment
There was a problem hiding this 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.
|
@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
} |
|
@jeevatkm |
jeevatkm
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@viennadd Thank you 😄
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