File tree 1 file changed +13
-4
lines changed
1 file changed +13
-4
lines changed Original file line number Diff line number Diff line change @@ -169,17 +169,26 @@ func WithQueryDel(key string) RequestOption {
169
169
// [sjson format]: https://github.com/tidwall/sjson
170
170
func WithJSONSet (key string , value interface {}) RequestOption {
171
171
return requestconfig .RequestOptionFunc (func (r * requestconfig.RequestConfig ) (err error ) {
172
- if buffer , ok := r .Body .(* bytes.Buffer ); ok {
173
- b := buffer .Bytes ()
172
+ var b []byte
173
+
174
+ if r .Body == nil {
175
+ b , err = sjson .SetBytes (nil , key , value )
176
+ if err != nil {
177
+ return err
178
+ }
179
+ } else if buffer , ok := r .Body .(* bytes.Buffer ); ok {
180
+ b = buffer .Bytes ()
174
181
b , err = sjson .SetBytes (b , key , value )
175
182
if err != nil {
176
183
return err
177
184
}
178
- r .Body = bytes .NewBuffer (b )
179
185
return nil
186
+ } else {
187
+ return fmt .Errorf ("cannot use WithJSONSet on a body that is not serialized as *bytes.Buffer" )
180
188
}
181
189
182
- return fmt .Errorf ("cannot use WithJSONSet on a body that is not serialized as *bytes.Buffer" )
190
+ r .Body = bytes .NewBuffer (b )
191
+ return nil
183
192
})
184
193
}
185
194
You can’t perform that action at this time.
0 commit comments