@@ -25,7 +25,7 @@ import (
25
25
26
26
const (
27
27
// ClientVersion is used in User-Agent request header to provide server with API level.
28
- ClientVersion = "5.1.0 "
28
+ ClientVersion = "5.1.1 "
29
29
30
30
// Endpoint points you to MessageBird REST API.
31
31
Endpoint = "https://rest.messagebird.com"
@@ -47,6 +47,14 @@ type Client struct {
47
47
DebugLog * log.Logger // Optional logger for debugging purposes
48
48
}
49
49
50
+ type contentType string
51
+
52
+ const (
53
+ contentTypeEmpty contentType = ""
54
+ contentTypeJSON contentType = "application/json"
55
+ contentTypeFormURLEncoded contentType = "application/x-www-form-urlencoded"
56
+ )
57
+
50
58
// New creates a new MessageBird client object.
51
59
func New (accessKey string ) * Client {
52
60
return & Client {
@@ -67,27 +75,26 @@ func (c *Client) Request(v interface{}, method, path string, data interface{}) e
67
75
return err
68
76
}
69
77
70
- var jsonEncoded []byte
71
- if data != nil {
72
- jsonEncoded , err = json .Marshal (data )
73
- if err != nil {
74
- return err
75
- }
78
+ body , contentType , err := prepareRequestBody (data )
79
+ if err != nil {
80
+ return err
76
81
}
77
82
78
- request , err := http .NewRequest (method , uri .String (), bytes .NewBuffer (jsonEncoded ))
83
+ request , err := http .NewRequest (method , uri .String (), bytes .NewBuffer (body ))
79
84
if err != nil {
80
85
return err
81
86
}
82
87
83
- request .Header .Set ("Content-Type" , "application/json" )
84
88
request .Header .Set ("Accept" , "application/json" )
85
89
request .Header .Set ("Authorization" , "AccessKey " + c .AccessKey )
86
90
request .Header .Set ("User-Agent" , "MessageBird/ApiClient/" + ClientVersion + " Go/" + runtime .Version ())
91
+ if contentType != contentTypeEmpty {
92
+ request .Header .Set ("Content-Type" , string (contentType ))
93
+ }
87
94
88
95
if c .DebugLog != nil {
89
96
if data != nil {
90
- c .DebugLog .Printf ("HTTP REQUEST: %s %s %s" , method , uri .String (), jsonEncoded )
97
+ c .DebugLog .Printf ("HTTP REQUEST: %s %s %s" , method , uri .String (), body )
91
98
} else {
92
99
c .DebugLog .Printf ("HTTP REQUEST: %s %s" , method , uri .String ())
93
100
}
@@ -136,3 +143,22 @@ func (c *Client) Request(v interface{}, method, path string, data interface{}) e
136
143
return errorResponse
137
144
}
138
145
}
146
+
147
+ // prepareRequestBody takes untyped data and attempts constructing a meaningful
148
+ // request body from it. It also returns the appropriate Content-Type.
149
+ func prepareRequestBody (data interface {}) ([]byte , contentType , error ) {
150
+ switch data := data .(type ) {
151
+ case nil :
152
+ // Nil bodies are accepted by `net/http`, so this is not an error.
153
+ return nil , contentTypeEmpty , nil
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
+ }
161
+
162
+ return b , contentTypeJSON , nil
163
+ }
164
+ }
0 commit comments