Documentation ¶
Overview ¶
Package sling is a Go REST client library for creating and sending requests.
Slings store http Request properties to simplify sending requests and decoding responses. Check the examples to learn how to compose a Sling into your API client.
Use a simple Sling to set request properties (Path, QueryParams, etc.) and then create a new http.Request by calling Request().
req, err := sling.New().Get("https://example.com").Request() client.Do(req)
Slings are much more powerful though. Use them to create REST clients which wrap complex API endpoints. Copy a base Sling with New() to avoid repeating common configuration.
const twitterApi = "https://https://api.twitter.com/1.1/" base := sling.New().Base(twitterApi).Client(httpAuthClient) users := base.New().Path("users/") statuses := base.New().Path("statuses/")
Choose an http method, set query parameters, and send the request.
statuses.New().Get("show.json").QueryStruct(params).Receive(tweet)
The usage README provides more details about setting headers, query parameters, body data, and decoding a typed response after sending.
Index ¶
- Constants
- type Sling
- func (s *Sling) Add(key, value string) *Sling
- func (s *Sling) Base(rawURL string) *Sling
- func (s *Sling) BodyStruct(bodyStruct interface{}) *Sling
- func (s *Sling) Client(httpClient *http.Client) *Sling
- func (s *Sling) Delete(pathURL string) *Sling
- func (s *Sling) Do(req *http.Request, v interface{}) (*http.Response, error)
- func (s *Sling) Get(pathURL string) *Sling
- func (s *Sling) Head(pathURL string) *Sling
- func (s *Sling) JsonBody(jsonBody interface{}) *Sling
- func (s *Sling) New() *Sling
- func (s *Sling) Patch(pathURL string) *Sling
- func (s *Sling) Path(path string) *Sling
- func (s *Sling) Post(pathURL string) *Sling
- func (s *Sling) Put(pathURL string) *Sling
- func (s *Sling) QueryStruct(queryStruct interface{}) *Sling
- func (s *Sling) Receive(v interface{}) (*http.Response, error)
- func (s *Sling) Request() (*http.Request, error)
- func (s *Sling) Set(key, value string) *Sling
Constants ¶
const ( HEAD = "HEAD" GET = "GET" POST = "POST" PUT = "PUT" PATCH = "PATCH" DELETE = "DELETE" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Sling ¶
type Sling struct { // http Client for doing requests HttpClient *http.Client // HTTP method (GET, POST, etc.) Method string // raw url string for requests RawUrl string // stores key-values pairs to add to request's Headers Header http.Header // contains filtered or unexported fields }
Sling is an HTTP Request builder and sender.
func (*Sling) Add ¶
Add adds the key, value pair in Headers, appending values for existing keys to the key's values. Header keys are canonicalized.
func (*Sling) Base ¶
Base sets the RawUrl. If you intend to extend the url with Path, baseUrl should be specified with a trailing slash.
func (*Sling) BodyStruct ¶
BodyStruct sets the Sling's bodyStruct. The value pointed to by the bodyStruct will be url encoded to set the Body on new requests. The bodyStruct argument should be a pointer to a url tagged struct. See https://godoc.org/github.com/google/go-querystring/query for details.
func (*Sling) Client ¶
Client sets the http Client used to do requests. If a nil client is given, the http.DefaultClient will be used.
func (*Sling) Do ¶
Do sends the HTTP request and decodes the response into the value pointed to by v. It wraps http.Client.Do, but handles closing the Response Body. The Response and any error doing the request are returned.
Note that non-2xx StatusCodes are valid responses, not errors.
func (*Sling) JsonBody ¶
JsonBody sets the Sling's jsonBody. The value pointed to by the jsonBody will be JSON encoded to set the Body on new requests (see Request()). The jsonBody argument should be a pointer to a json tagged struct. See https://golang.org/pkg/encoding/json/#MarshalIndent for details.
func (*Sling) New ¶
New returns a copy of a Sling for creating a new Sling with properties from a base Sling. For example,
baseSling := sling.New().Client(client).Base("https://api.io/") fooSling := baseSling.New().Get("foo/") barSling := baseSling.New().Get("bar/")
fooSling and barSling will both use the same client, but send requests to https://api.io/foo/ and https://api.io/bar/ respectively.
Note that jsonBody and queryStructs item values are copied so if pointer values are used, mutating the original value will mutate the value within the child Sling.
func (*Sling) Path ¶
Path extends the RawUrl with the given path by resolving the reference to an absolute URL. If parsing errors occur, the RawUrl is left unmodified.
func (*Sling) QueryStruct ¶
QueryStruct appends the queryStruct to the Sling's queryStructs. The value pointed to by each queryStruct will be encoded as url query parameters on new requests (see Request()). The queryStruct argument should be a pointer to a url tagged struct. See https://godoc.org/github.com/google/go-querystring/query for details.
func (*Sling) Receive ¶
Receive creates a new HTTP request, sends it, and decodes the response into the value pointed to by v. Receive is shorthand for calling Request and Do.