Skip to content

Commit 7b17389

Browse files
committed
Change prepareRequestBody to be more readable
1 parent 1971187 commit 7b17389

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

client.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -147,20 +147,18 @@ func (c *Client) Request(v interface{}, method, path string, data interface{}) e
147147
// prepareRequestBody takes untyped data and attempts constructing a meaningful
148148
// request body from it. It also returns the appropriate Content-Type.
149149
func prepareRequestBody(data interface{}) ([]byte, contentType, error) {
150-
// Nil bodies are accepted by `net/http`, so this is not an error.
151-
if data == nil {
150+
switch data := data.(type) {
151+
case nil:
152+
// Nil bodies are accepted by `net/http`, so this is not an error.
152153
return nil, contentTypeEmpty, nil
153-
}
154-
155-
s, ok := data.(string)
156-
if ok {
157-
return []byte(s), contentTypeFormURLEncoded, nil
158-
}
154+
case string:
155+
return []byte(data), contentTypeFormURLEncoded, nil
156+
default:
157+
b, err := json.Marshal(data)
158+
if err != nil {
159+
return nil, contentType(""), err
160+
}
159161

160-
b, err := json.Marshal(data)
161-
if err != nil {
162-
return nil, contentType(""), err
162+
return b, contentTypeJSON, nil
163163
}
164-
165-
return b, contentTypeJSON, nil
166164
}

0 commit comments

Comments
 (0)