-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
suzhen
committed
Mar 20, 2021
1 parent
d9d7e7d
commit 5676563
Showing
3 changed files
with
19 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,27 @@ | ||
package internal | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"github.com/guestin/go-requests/opt" | ||
"io" | ||
) | ||
|
||
func DumpResponse(tag string) opt.Option { | ||
return func(context *opt.RequestContext) error { | ||
old := context.ResponseHandler | ||
context.ResponseHandler = func(statusCode int, stream io.Reader) (interface{}, error) { | ||
fmt.Printf("[%s] status code=%d\n", tag, statusCode) | ||
respBytes, err := io.ReadAll(stream) | ||
if err != nil { | ||
fmt.Printf("[%s] read response error:%s\n", tag, err) | ||
return nil, err | ||
} | ||
fmt.Printf("[%s] response:%s\n", tag, respBytes) | ||
if old != nil { | ||
return old(statusCode, bytes.NewReader(respBytes)) | ||
} | ||
return nil, nil | ||
} | ||
func DumpResponse(tag string, skip bool) opt.Option { | ||
return func(reqCtx *opt.RequestContext) error { | ||
reqCtx.InstallResponseHandler( | ||
func(statusCode int, stream io.Reader, previousValue interface{}) (interface{}, error) { | ||
if skip { | ||
return previousValue, nil | ||
} | ||
fmt.Printf("[%s] status code=%d\n", tag, statusCode) | ||
respBytes, err := io.ReadAll(stream) | ||
if err != nil { | ||
fmt.Printf("[%s] read response error:%s\n", tag, err) | ||
return nil, err | ||
} | ||
fmt.Printf("[%s] response:%s\n", tag, respBytes) | ||
return nil, nil | ||
}, opt.HEAD) | ||
return nil | ||
} | ||
|
||
} |