Skip to content
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

feat: add SetEscapeHTML to switch wether close or open escape html #188

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions gorequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ type SuperAgent struct {
BasicAuth struct{ Username, Password string }
Debug bool
CurlCommand bool
EscapeHTML bool
logger Logger
Retryable superAgentRetryable
DoNotClearSuperAgent bool
Expand Down Expand Up @@ -116,6 +117,7 @@ func New() *SuperAgent {
BasicAuth: struct{ Username, Password string }{},
Debug: debug,
CurlCommand: false,
EscapeHTML: true,
logger: log.New(os.Stderr, "[gorequest]", log.LstdFlags),
isClone: false,
}
Expand Down Expand Up @@ -245,6 +247,12 @@ func (s *SuperAgent) SetCurlCommand(enable bool) *SuperAgent {
return s
}

// SetEscapeHTML escape html by default, set to false to disable
func (s *SuperAgent) SetEscapeHTML(escapeHTML bool) *SuperAgent {
s.EscapeHTML = escapeHTML
return s
}

// Enable the DoNotClear mode for not clearing super agent and reuse for the next request
func (s *SuperAgent) SetDoNotClearSuperAgent(enable bool) *SuperAgent {
s.DoNotClearSuperAgent = enable
Expand Down Expand Up @@ -528,7 +536,7 @@ func (s *SuperAgent) Query(content interface{}) *SuperAgent {
}

func (s *SuperAgent) queryStruct(content interface{}) *SuperAgent {
if marshalContent, err := json.Marshal(content); err != nil {
if marshalContent, err := s.encodeJson(content); err != nil {
s.Errors = append(s.Errors, err)
} else {
var val map[string]interface{}
Expand All @@ -546,7 +554,7 @@ func (s *SuperAgent) queryStruct(content interface{}) *SuperAgent {
case time.Time:
queryVal = t.Format(time.RFC3339)
default:
j, err := json.Marshal(v)
j, err := s.encodeJson(v)
if err != nil {
continue
}
Expand Down Expand Up @@ -759,7 +767,7 @@ func (s *SuperAgent) SendMap(content interface{}) *SuperAgent {
// SendStruct (similar to SendString) returns SuperAgent's itself for any next chain and takes content interface{} as a parameter.
// Its duty is to transfrom interface{} (implicitly always a struct) into s.Data (map[string]interface{}) which later changes into appropriate format such as json, form, text, etc. in the End() func.
func (s *SuperAgent) SendStruct(content interface{}) *SuperAgent {
if marshalContent, err := json.Marshal(content); err != nil {
if marshalContent, err := s.encodeJson(content); err != nil {
s.Errors = append(s.Errors, err)
} else {
var val map[string]interface{}
Expand Down Expand Up @@ -1257,9 +1265,9 @@ func (s *SuperAgent) MakeRequest() (*http.Request, error) {
if s.BounceToRawString {
contentJson = []byte(s.RawString)
} else if len(s.Data) != 0 {
contentJson, _ = json.Marshal(s.Data)
contentJson, _ = s.encodeJson(s.Data)
} else if len(s.SliceData) != 0 {
contentJson, _ = json.Marshal(s.SliceData)
contentJson, _ = s.encodeJson(s.SliceData)
}
if contentJson != nil {
contentReader = bytes.NewReader(contentJson)
Expand Down Expand Up @@ -1325,7 +1333,7 @@ func (s *SuperAgent) MakeRequest() (*http.Request, error) {
h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%s"`, fieldName))
h.Set("Content-Type", "application/json")
fw, _ := mw.CreatePart(h)
contentJson, err := json.Marshal(s.SliceData)
contentJson, err := s.encodeJson(s.SliceData)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1408,3 +1416,16 @@ func (s *SuperAgent) AsCurlCommand() (string, error) {
}
return cmd.String(), nil
}

// Encode writes the JSON encoding of v to the stream,
func (s *SuperAgent) encodeJson(val interface{}) ([]byte, error) {
buf := new(bytes.Buffer)
encoder := json.NewEncoder(buf)
encoder.SetEscapeHTML(s.EscapeHTML)

if err := encoder.Encode(val); err != nil {
return nil, err
}

return buf.Bytes(), nil
}