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 1 commit
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
Next Next commit
feat: add SetEscapeHTML to switch wether close or open escape html
  • Loading branch information
maskwang committed Apr 14, 2018
commit 2d1d2d127c5af819f21a8ccd43a5146eaf1b91a2
33 changes: 27 additions & 6 deletions gorequest.go
Original file line number Diff line number Diff line change
@@ -80,6 +80,7 @@ type SuperAgent struct {
BasicAuth struct{ Username, Password string }
Debug bool
CurlCommand bool
EscapeHTML bool
logger Logger
Retryable struct {
RetryableStatus []int
@@ -120,6 +121,7 @@ func New() *SuperAgent {
BasicAuth: struct{ Username, Password string }{},
Debug: debug,
CurlCommand: false,
EscapeHTML: true,
logger: log.New(os.Stderr, "[gorequest]", log.LstdFlags),
}
// disable keep alives by default, see this issue https://github.com/parnurzeal/gorequest/issues/75
@@ -139,6 +141,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
@@ -422,7 +430,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{}
@@ -440,7 +448,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
}
@@ -662,7 +670,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{}
@@ -1158,9 +1166,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)
@@ -1226,7 +1234,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
}
@@ -1310,3 +1318,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
}