1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
package kong
import (
"bytes"
"encoding/json"
"errors"
"net/http"
"github.com/google/go-querystring/query"
)
// NewRequest creates a request based on the inputs.
// endpoint should be relative to the baseURL specified during
// client creation.
// body is always marshaled into JSON.
func (c *Client) NewRequest(method, endpoint string, qs interface{},
body interface{}) (*http.Request, error) {
if endpoint == "" {
return nil, errors.New("endpoint can't be nil")
}
//body to be sent in JSON
var buf []byte
if body != nil {
var err error
buf, err = json.Marshal(body)
if err != nil {
return nil, err
}
}
//Create a new request
req, err := http.NewRequest(method, c.baseURL+endpoint,
bytes.NewBuffer(buf))
if err != nil {
return nil, err
}
// add body if needed
if body != nil {
req.Header.Add("Content-Type", "application/json")
}
// add query string if any
if qs != nil {
values, err := query.Values(qs)
if err != nil {
return nil, err
}
req.URL.RawQuery = values.Encode()
}
return req, nil
}
|