Skip to content

Commit

Permalink
Don't send an empty body when the data is nil
Browse files Browse the repository at this point in the history
  • Loading branch information
Renaud Amar committed Dec 15, 2017
1 parent ceb28d2 commit b6f66fa
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions jcapi.go
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"reflect"
Expand Down Expand Up @@ -161,7 +162,13 @@ func (jc JCAPI) Do(op, url string, data []byte) (interface{}, JCError) {

client := &http.Client{}

req, err := http.NewRequest(op, fullUrl, bytes.NewReader(data))
// if there is no data, we should send a nil body, not an empty one:
var body io.Reader
if len(data) > 0 {
body = bytes.NewReader(data)
}

req, err := http.NewRequest(op, fullUrl, body)
if err != nil {
return returnVal, fmt.Errorf("ERROR: Could not build search request: '%s'", err)
}
Expand Down Expand Up @@ -198,7 +205,14 @@ func (jc JCAPI) DoBytes(op, urlQuery string, data []byte) ([]byte, JCError) {

client := &http.Client{}

req, err := http.NewRequest(op, fullUrl, bytes.NewReader(data))
// if there is no data, we should send a nil body, not an empty one:
var body io.Reader
if len(data) > 0 {
body = bytes.NewReader(data)
}

req, err := http.NewRequest(op, fullUrl, body)

if err != nil {
return nil, fmt.Errorf("ERROR: Could not build search request: '%s'", err)
}
Expand Down

0 comments on commit b6f66fa

Please sign in to comment.