1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-29 17:01:27 +00:00

Merge pull request #1768 from aolu11/master

fix json extra newline
This commit is contained in:
astaxie 2016-03-09 21:11:20 +08:00
commit 589616b303

View File

@ -301,13 +301,12 @@ func (b *BeegoHTTPRequest) Body(data interface{}) *BeegoHTTPRequest {
// JSONBody adds request raw body encoding by JSON. // JSONBody adds request raw body encoding by JSON.
func (b *BeegoHTTPRequest) JSONBody(obj interface{}) (*BeegoHTTPRequest, error) { func (b *BeegoHTTPRequest) JSONBody(obj interface{}) (*BeegoHTTPRequest, error) {
if b.req.Body == nil && obj != nil { if b.req.Body == nil && obj != nil {
buf := bytes.NewBuffer(nil) byts, err := json.Marshal(obj)
enc := json.NewEncoder(buf) if err != nil {
if err := enc.Encode(obj); err != nil {
return b, err return b, err
} }
b.req.Body = ioutil.NopCloser(buf) b.req.Body = ioutil.NopCloser(bytes.NewReader(byts))
b.req.ContentLength = int64(buf.Len()) b.req.ContentLength = int64(len(byts))
b.req.Header.Set("Content-Type", "application/json") b.req.Header.Set("Content-Type", "application/json")
} }
return b, nil return b, nil