Skip to content

Commit 7dda615

Browse files
authored
Adding ability to customize the XML marshal/unmarshal functions #481 (#484)
1 parent d837dfc commit 7dda615

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

client.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"crypto/tls"
1111
"crypto/x509"
1212
"encoding/json"
13+
"encoding/xml"
1314
"errors"
1415
"fmt"
1516
"io"
@@ -114,6 +115,8 @@ type Client struct {
114115
RetryAfter RetryAfterFunc
115116
JSONMarshal func(v interface{}) ([]byte, error)
116117
JSONUnmarshal func(data []byte, v interface{}) error
118+
XMLMarshal func(v interface{}) ([]byte, error)
119+
XMLUnmarshal func(data []byte, v interface{}) error
117120

118121
// HeaderAuthorizationKey is used to set/access Request Authorization header
119122
// value when `SetAuthToken` option is used.
@@ -1074,13 +1077,16 @@ func createClient(hc *http.Client) *Client {
10741077
Cookies: make([]*http.Cookie, 0),
10751078
RetryWaitTime: defaultWaitTime,
10761079
RetryMaxWaitTime: defaultMaxWaitTime,
1080+
PathParams: make(map[string]string),
10771081
JSONMarshal: json.Marshal,
10781082
JSONUnmarshal: json.Unmarshal,
1083+
XMLMarshal: xml.Marshal,
1084+
XMLUnmarshal: xml.Unmarshal,
10791085
HeaderAuthorizationKey: http.CanonicalHeaderKey("Authorization"),
1080-
jsonEscapeHTML: true,
1081-
httpClient: hc,
1082-
debugBodySizeLimit: math.MaxInt32,
1083-
PathParams: make(map[string]string),
1086+
1087+
jsonEscapeHTML: true,
1088+
httpClient: hc,
1089+
debugBodySizeLimit: math.MaxInt32,
10841090
}
10851091

10861092
// Logger

middleware.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package resty
66

77
import (
88
"bytes"
9-
"encoding/xml"
109
"errors"
1110
"fmt"
1211
"io"
@@ -462,7 +461,7 @@ func handleRequestBody(c *Client, r *Request) (err error) {
462461
return
463462
}
464463
} else if IsXMLType(contentType) && (kind == reflect.Struct) {
465-
bodyBytes, err = xml.Marshal(r.Body)
464+
bodyBytes, err = c.XMLMarshal(r.Body)
466465
if err != nil {
467466
return
468467
}

util.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package resty
66

77
import (
88
"bytes"
9-
"encoding/xml"
109
"fmt"
1110
"io"
1211
"log"
@@ -109,7 +108,7 @@ func Unmarshalc(c *Client, ct string, b []byte, d interface{}) (err error) {
109108
if IsJSONType(ct) {
110109
err = c.JSONUnmarshal(b, d)
111110
} else if IsXMLType(ct) {
112-
err = xml.Unmarshal(b, d)
111+
err = c.XMLUnmarshal(b, d)
113112
}
114113

115114
return

0 commit comments

Comments
 (0)