Skip to content

Commit

Permalink
fix print noescape json body
Browse files Browse the repository at this point in the history
  • Loading branch information
Tian Pengfei committed May 30, 2022
1 parent 3d08b36 commit 695a451
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion request.go
Expand Up @@ -832,7 +832,11 @@ func (r *Request) fmtBodyString(sl int64) (body string) {
contentType := r.Header.Get(hdrContentTypeKey)
kind := kindOf(r.Body)
if canJSONMarshal(contentType, kind) {
prtBodyBytes, err = json.MarshalIndent(&r.Body, "", " ")
if !r.jsonEscapeHTML || !r.client.jsonEscapeHTML {
prtBodyBytes = noescapeJSONMarshalIndent(&r.Body, "", " ")
}else{
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 {
Expand Down Expand Up @@ -894,3 +898,15 @@ var noescapeJSONMarshal = func(v interface{}) (*bytes.Buffer, error) {

return buf, nil
}
var noescapeJSONMarshalIndent = func(v interface{}, prefix, indent string) (*bytes.Buffer, error) {
buf := acquireBuffer()
encoder := json.NewEncoder(buf)
encoder.SetEscapeHTML(false)
encoder.SetIndent(prefix, indent)
if err := encoder.Encode(v); err != nil {
releaseBuffer(buf)
return nil, err
}

return buf, nil
}

0 comments on commit 695a451

Please sign in to comment.