Skip to content

Commit 31d1eb4

Browse files
committed
SetBody now supports pointer too.
1 parent 1009ace commit 31d1eb4

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

middleware.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,14 @@ func parseRequestBody(c *Client, r *Request) (err error) {
137137

138138
var bodyBytes []byte
139139
kind := reflect.ValueOf(r.Body).Kind()
140+
if kind == reflect.Ptr {
141+
kind = reflect.TypeOf(r.Body).Elem().Kind()
142+
}
143+
140144
if IsJSONType(contentType) && (kind == reflect.Struct || kind == reflect.Map) {
141-
bodyBytes, err = json.Marshal(&r.Body)
145+
bodyBytes, err = json.Marshal(r.Body)
142146
} else if IsXMLType(contentType) && (kind == reflect.Struct) {
143-
bodyBytes, err = xml.Marshal(&r.Body)
147+
bodyBytes, err = xml.Marshal(r.Body)
144148
} else if b, ok := r.Body.(string); ok {
145149
bodyBytes = []byte(b)
146150
} else if b, ok := r.Body.([]byte); ok {

resty_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,11 @@ func TestPostJSONStructSuccess(t *testing.T) {
175175
ts := createPostServer(t)
176176
defer ts.Close()
177177

178+
user := &User{Username: "testuser", Password: "testpass"}
179+
178180
resp, err := dclr().
179181
SetHeader(hdrContentTypeKey, jsonContentType).
180-
SetBody(User{Username: "testuser", Password: "testpass"}).
182+
SetBody(user).
181183
SetResult(&AuthSuccess{}).
182184
Post(ts.URL + "/login")
183185

0 commit comments

Comments
 (0)