Skip to content

Commit

Permalink
SetBody now supports pointer too.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed Oct 5, 2015
1 parent 1009ace commit 31d1eb4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
8 changes: 6 additions & 2 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,14 @@ func parseRequestBody(c *Client, r *Request) (err error) {

var bodyBytes []byte
kind := reflect.ValueOf(r.Body).Kind()
if kind == reflect.Ptr {
kind = reflect.TypeOf(r.Body).Elem().Kind()
}

if IsJSONType(contentType) && (kind == reflect.Struct || kind == reflect.Map) {
bodyBytes, err = json.Marshal(&r.Body)
bodyBytes, err = json.Marshal(r.Body)
} else if IsXMLType(contentType) && (kind == reflect.Struct) {
bodyBytes, err = xml.Marshal(&r.Body)
bodyBytes, err = xml.Marshal(r.Body)
} else if b, ok := r.Body.(string); ok {
bodyBytes = []byte(b)
} else if b, ok := r.Body.([]byte); ok {
Expand Down
4 changes: 3 additions & 1 deletion resty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,11 @@ func TestPostJSONStructSuccess(t *testing.T) {
ts := createPostServer(t)
defer ts.Close()

user := &User{Username: "testuser", Password: "testpass"}

resp, err := dclr().
SetHeader(hdrContentTypeKey, jsonContentType).
SetBody(User{Username: "testuser", Password: "testpass"}).
SetBody(user).
SetResult(&AuthSuccess{}).
Post(ts.URL + "/login")

Expand Down

0 comments on commit 31d1eb4

Please sign in to comment.