Skip to content

Commit

Permalink
Adding ability to customize the XML marshal/unmarshal functions #481
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed Oct 31, 2021
1 parent d837dfc commit 5965565
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
14 changes: 10 additions & 4 deletions client.go
Expand Up @@ -10,6 +10,7 @@ import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -114,6 +115,8 @@ type Client struct {
RetryAfter RetryAfterFunc
JSONMarshal func(v interface{}) ([]byte, error)
JSONUnmarshal func(data []byte, v interface{}) error
XMLMarshal func(v interface{}) ([]byte, error)
XMLUnmarshal func(data []byte, v interface{}) error

// HeaderAuthorizationKey is used to set/access Request Authorization header
// value when `SetAuthToken` option is used.
Expand Down Expand Up @@ -1074,13 +1077,16 @@ func createClient(hc *http.Client) *Client {
Cookies: make([]*http.Cookie, 0),
RetryWaitTime: defaultWaitTime,
RetryMaxWaitTime: defaultMaxWaitTime,
PathParams: make(map[string]string),
JSONMarshal: json.Marshal,
JSONUnmarshal: json.Unmarshal,
XMLMarshal: xml.Marshal,
XMLUnmarshal: xml.Unmarshal,
HeaderAuthorizationKey: http.CanonicalHeaderKey("Authorization"),
jsonEscapeHTML: true,
httpClient: hc,
debugBodySizeLimit: math.MaxInt32,
PathParams: make(map[string]string),

jsonEscapeHTML: true,
httpClient: hc,
debugBodySizeLimit: math.MaxInt32,
}

// Logger
Expand Down
3 changes: 1 addition & 2 deletions middleware.go
Expand Up @@ -6,7 +6,6 @@ package resty

import (
"bytes"
"encoding/xml"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -462,7 +461,7 @@ func handleRequestBody(c *Client, r *Request) (err error) {
return
}
} else if IsXMLType(contentType) && (kind == reflect.Struct) {
bodyBytes, err = xml.Marshal(r.Body)
bodyBytes, err = c.XMLMarshal(r.Body)
if err != nil {
return
}
Expand Down
3 changes: 1 addition & 2 deletions util.go
Expand Up @@ -6,7 +6,6 @@ package resty

import (
"bytes"
"encoding/xml"
"fmt"
"io"
"log"
Expand Down Expand Up @@ -109,7 +108,7 @@ func Unmarshalc(c *Client, ct string, b []byte, d interface{}) (err error) {
if IsJSONType(ct) {
err = c.JSONUnmarshal(b, d)
} else if IsXMLType(ct) {
err = xml.Unmarshal(b, d)
err = c.XMLUnmarshal(b, d)
}

return
Expand Down

0 comments on commit 5965565

Please sign in to comment.