add JsonBody
Showing
1 changed file
with
16 additions
and
0 deletions
| ... | @@ -253,6 +253,22 @@ func (b *BeegoHttpRequest) Body(data interface{}) *BeegoHttpRequest { | ... | @@ -253,6 +253,22 @@ func (b *BeegoHttpRequest) Body(data interface{}) *BeegoHttpRequest { |
| 253 | return b | 253 | return b |
| 254 | } | 254 | } |
| 255 | 255 | ||
| 256 | |||
| 257 | // JsonBody adds request raw body encoding by JSON. | ||
| 258 | func (b *BeegoHttpRequest) JsonBody(obj interface{}) (*BeegoHttpRequest, error) { | ||
| 259 | if b.req.Body == nil && obj != nil { | ||
| 260 | buf := bytes.NewBuffer(nil) | ||
| 261 | enc := json.NewEncoder(buf) | ||
| 262 | if err := enc.Encode(obj); err != nil { | ||
| 263 | return b, err | ||
| 264 | } | ||
| 265 | b.req.Body = ioutil.NopCloser(buf) | ||
| 266 | b.req.ContentLength = int64(buf.Len()) | ||
| 267 | b.req.Header.Set("Content-Type", "application/json") | ||
| 268 | } | ||
| 269 | return b, nil | ||
| 270 | } | ||
| 271 | |||
| 256 | func (b *BeegoHttpRequest) buildUrl(paramBody string) { | 272 | func (b *BeegoHttpRequest) buildUrl(paramBody string) { |
| 257 | // build GET url with query string | 273 | // build GET url with query string |
| 258 | if b.req.Method == "GET" && len(paramBody) > 0 { | 274 | if b.req.Method == "GET" && len(paramBody) > 0 { | ... | ... |
-
Please register or sign in to post a comment