Documentation ¶
Index ¶
- Variables
- func Apply(request *http.Request, options ...Option) (*http.Request, error)
- func Connect(url string, options ...Option) (*http.Response, error)
- func Delete(url string, options ...Option) (*http.Response, error)
- func Do(method, url string, options ...Option) (*http.Response, error)
- func Get(url string, options ...Option) (*http.Response, error)
- func Head(url string, options ...Option) (*http.Response, error)
- func New(method, url string, options ...Option) (*http.Request, error)
- func NewConnect(url string, options ...Option) (*http.Request, error)
- func NewDelete(url string, options ...Option) (*http.Request, error)
- func NewGet(url string, options ...Option) (*http.Request, error)
- func NewHead(url string, options ...Option) (*http.Request, error)
- func NewOptions(url string, options ...Option) (*http.Request, error)
- func NewPatch(url string, options ...Option) (*http.Request, error)
- func NewPost(url string, options ...Option) (*http.Request, error)
- func NewPut(url string, options ...Option) (*http.Request, error)
- func NewTrace(url string, options ...Option) (*http.Request, error)
- func Options(url string, options ...Option) (*http.Response, error)
- func Patch(url string, options ...Option) (*http.Response, error)
- func Post(url string, options ...Option) (*http.Response, error)
- func Put(url string, options ...Option) (*http.Response, error)
- func Trace(url string, options ...Option) (*http.Response, error)
- type BodyForm
- type Client
- func (c *Client) Connect(options ...Option) (*http.Response, error)
- func (c *Client) Delete(options ...Option) (*http.Response, error)
- func (c *Client) Do(method string, options ...Option) (*http.Response, error)
- func (c *Client) Get(options ...Option) (*http.Response, error)
- func (c *Client) Head(options ...Option) (*http.Response, error)
- func (c *Client) Options(options ...Option) (*http.Response, error)
- func (c *Client) Patch(options ...Option) (*http.Response, error)
- func (c *Client) Post(options ...Option) (*http.Response, error)
- func (c *Client) Put(options ...Option) (*http.Response, error)
- func (c *Client) Trace(options ...Option) (*http.Response, error)
- type Doer
- type Headers
- type Option
- func Accept(accept string) Option
- func Authorization(authorization string) Option
- func BasicAuth(username, password string) Option
- func BearerAuth(token string) Option
- func Body(body io.ReadCloser) Option
- func BodyBytes(body []byte) Option
- func BodyJSON(v interface{}) Option
- func BodyReader(body io.Reader) Option
- func BodyString(body string) Option
- func BodyXML(v interface{}) Option
- func ContentType(contentType string) Option
- func Context(ctx context.Context) Option
- func ContextValue(key, value interface{}) Option
- func Cookie(cookie *http.Cookie) Option
- func Dump(w io.Writer) Option
- func Header(key, value string) Option
- func Host(host string) Option
- func Path(segments ...string) Option
- func Query(key, value string) Option
- func RawURL(url *pkgurl.URL) Option
- func Referer(referer string) Option
- func Scheme(scheme string) Option
- func TokenAuth(token string) Option
- func URL(url string) Option
- func User(user *pkgurl.Userinfo) Option
- func UserAgent(userAgent string) Option
- func UserPassword(username, password string) Option
- func Username(username string) Option
- type OptionFunc
- type Pipeline
- type Queries
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultClient = NewClient(http.DefaultClient)
DefaultClient captures the current Doer.
Functions ¶
func Connect ¶
Connect makes a CONNECT request using the current DefaultClient and returns the *http.Response.
func Delete ¶
Delete makes a DELETE request using the current DefaultClient and returns the *http.Response.
func Do ¶
Do makes an *http.Request using the current DefaultClient and returns the *http.Response.
Example ¶
package main import ( "fmt" "io/ioutil" "net/http" "net/http/httptest" "github.com/broothie/qst" ) func main() { server := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { fmt.Println(r.Method) fmt.Println(r.URL) fmt.Println(r.Header.Get("Authorization")) body, _ := ioutil.ReadAll(r.Body) fmt.Println(string(body)) })) defer server.Close() qst.Do(http.MethodPost, server.URL, qst.Path("api", "/cereals", "1234"), qst.BearerAuth("c0rnfl@k3s"), qst.BodyJSON(map[string]string{"name": "Honey Bunches of Oats"}), ) }
Output: POST /api/cereals/1234 Bearer c0rnfl@k3s {"name":"Honey Bunches of Oats"}
func Head ¶
Head makes a HEAD request using the current DefaultClient and returns the *http.Response.
func New ¶
New builds a new *http.Request.
Example ¶
package main import ( "fmt" "io/ioutil" "net/http" "github.com/broothie/qst" ) func main() { req, _ := qst.New(http.MethodPost, "http://bfast.com/api", qst.Scheme("https"), qst.Host("breakfast.com"), qst.Path("/cereals", "1234"), qst.BearerAuth("c0rnfl@k3s"), qst.BodyJSON(map[string]string{"name": "Honey Bunches of Oats"}), ) fmt.Println(req.Method) fmt.Println(req.URL) fmt.Println(req.Header.Get("Authorization")) body, _ := ioutil.ReadAll(req.Body) fmt.Println(string(body)) }
Output: POST https://breakfast.com/api/cereals/1234 Bearer c0rnfl@k3s {"name":"Honey Bunches of Oats"}
func NewConnect ¶
NewConnect builds a new *http.Request with method CONNECT.
func NewOptions ¶
NewOptions builds a new *http.Request with method OPTIONS.
func Options ¶
Options makes a OPTIONS request using the current DefaultClient and returns the *http.Response.
func Patch ¶
Patch makes a PATCH request using the current DefaultClient and returns the *http.Response.
func Post ¶
Post makes a POST request using the current DefaultClient and returns the *http.Response.
Types ¶
type BodyForm ¶
BodyForm URL-encodes multiple key/value pairs and applies the result to the *http.Request body.
Example ¶
package main import ( "fmt" "io/ioutil" "github.com/broothie/qst" ) func main() { request, _ := qst.NewPost("https://breakfast.com/api/cereals", qst.BodyForm{"name": {"Grape Nuts"}}, ) body, _ := ioutil.ReadAll(request.Body) fmt.Println(string(body)) }
Output: name=Grape+Nuts
type Client ¶
type Client struct { Pipeline // contains filtered or unexported fields }
Client captures a Doer and Options to apply to every request.
func NewClient ¶ added in v0.1.0
NewClient creates a new Client.
Example ¶
package main import ( "fmt" "net/http" "net/http/httptest" "github.com/broothie/qst" ) func main() { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Println(r.Header.Get("Authorization")) fmt.Println(r.URL.RawQuery) })) defer server.Close() client := qst.NewClient(http.DefaultClient, qst.URL(server.URL), qst.BearerAuth("c0rnfl@k3s"), ) client.Get(qst.Query("page", "10")) }
Output: Bearer c0rnfl@k3s page=10
func (*Client) Connect ¶
Connect makes a CONNECT request and returns the *http.Response using the Doer assigned to c.
func (*Client) Delete ¶
Delete makes a DELETE request and returns the *http.Response using the Doer assigned to c.
func (*Client) Do ¶
Do makes an *http.Request and returns the *http.Response using the Doer assigned to c.
func (*Client) Get ¶
Get makes a GET request and returns the *http.Response using the Doer assigned to c.
func (*Client) Head ¶
Head makes a HEAD request and returns the *http.Response using the Doer assigned to c.
func (*Client) Options ¶
Options makes a OPTIONS request and returns the *http.Response using the Doer assigned to c.
func (*Client) Patch ¶
Patch makes a PATCH request and returns the *http.Response using the Doer assigned to c.
func (*Client) Post ¶
Post makes a POST request and returns the *http.Response using the Doer assigned to c.
type Headers ¶
Headers applies multiple key/value pairs to the headers of the *http.Request. It wraps http.Header.
Example ¶
package main import ( "fmt" "github.com/broothie/qst" ) func main() { request, _ := qst.NewGet("https://breakfast.com/api/cereals", qst.Headers{ "grain": {"oats"}, "style": {"toasted"}, }, ) fmt.Println(request.Header) }
Output: map[Grain:[oats] Style:[toasted]]
type Option ¶
Option is an option for building an *http.Request.
func Accept ¶ added in v0.1.1
Accept applies an "Accept" header to the *http.Request.
Example ¶
package main import ( "fmt" "github.com/broothie/qst" ) func main() { request, _ := qst.NewGet("https://breakfast.com/api/cereals", qst.Accept("application/json"), ) fmt.Println(request.Header.Get("Accept")) }
Output: application/json
func Authorization ¶
Authorization applies an "Authorization" header to the *http.Request.
Example ¶
package main import ( "fmt" "github.com/broothie/qst" ) func main() { request, _ := qst.NewGet("https://breakfast.com/api/cereals", qst.Authorization("c0rnfl@k3s"), ) fmt.Println(request.Header.Get("Authorization")) }
Output: c0rnfl@k3s
func BasicAuth ¶
BasicAuth applies a username and password basic auth header.
Example ¶
package main import ( "fmt" "github.com/broothie/qst" ) func main() { request, _ := qst.NewGet("https://breakfast.com/api/cereals", qst.BasicAuth("TonyTheTiger", "grrreat"), ) fmt.Println(request.Header.Get("Authorization")) }
Output: Basic VG9ueVRoZVRpZ2VyOmdycnJlYXQ=
func BearerAuth ¶
BearerAuth applies an "Authorization: Bearer <token>" header to the *http.Request.
Example ¶
package main import ( "fmt" "github.com/broothie/qst" ) func main() { request, _ := qst.NewGet("https://breakfast.com/api/cereals", qst.BearerAuth("c0rnfl@k3s"), ) fmt.Println(request.Header.Get("Authorization")) }
Output: Bearer c0rnfl@k3s
func Body ¶
func Body(body io.ReadCloser) Option
Body applies an io.ReadCloser to the *http.Request body.
Example ¶
package main import ( "bytes" "fmt" "io/ioutil" "github.com/broothie/qst" ) func main() { request, _ := qst.NewPost("https://breakfast.com/api/cereals", qst.Body(ioutil.NopCloser(bytes.NewBufferString("Part of a complete breakfast."))), ) body, _ := ioutil.ReadAll(request.Body) fmt.Println(string(body)) }
Output: Part of a complete breakfast.
func BodyBytes ¶
BodyBytes applies a slice of bytes to the *http.Request body.
Example ¶
package main import ( "fmt" "io/ioutil" "github.com/broothie/qst" ) func main() { request, _ := qst.NewPost("https://breakfast.com/api/cereals", qst.BodyBytes([]byte("Part of a complete breakfast.")), ) body, _ := ioutil.ReadAll(request.Body) fmt.Println(string(body)) }
Output: Part of a complete breakfast.
func BodyJSON ¶
func BodyJSON(v interface{}) Option
BodyJSON encodes an object as JSON and applies it to the *http.Request body.
Example ¶
package main import ( "fmt" "io/ioutil" "github.com/broothie/qst" ) func main() { request, _ := qst.NewPost("https://breakfast.com/api/cereals", qst.BodyJSON(map[string]string{"name": "Rice Krispies"}), ) body, _ := ioutil.ReadAll(request.Body) fmt.Println(string(body)) }
Output: {"name":"Rice Krispies"}
func BodyReader ¶ added in v0.0.7
BodyReader applies an io.Reader to the *http.Request body.
func BodyString ¶
BodyString applies a string to the *http.Request body.
Example ¶
package main import ( "fmt" "io/ioutil" "github.com/broothie/qst" ) func main() { request, _ := qst.NewPost("https://breakfast.com/api/cereals", qst.BodyString("Part of a complete breakfast."), ) body, _ := ioutil.ReadAll(request.Body) fmt.Println(string(body)) }
Output: Part of a complete breakfast.
func BodyXML ¶
func BodyXML(v interface{}) Option
BodyXML encodes an object as XML and applies it to the *http.Request body.
Example ¶
package main import ( "fmt" "io/ioutil" "github.com/broothie/qst" ) func main() { request, _ := qst.NewPost("https://breakfast.com/api/cereals", qst.BodyXML("Part of a complete breakfast."), ) body, _ := ioutil.ReadAll(request.Body) fmt.Println(string(body)) }
Output: <string>Part of a complete breakfast.</string>
func ContentType ¶ added in v0.1.1
ContentType applies a "Content-Type" to the *http.Request.
Example ¶
package main import ( "fmt" "github.com/broothie/qst" ) func main() { request, _ := qst.NewGet("https://breakfast.com/api/cereals", qst.ContentType("application/json"), ) fmt.Println(request.Header.Get("Content-Type")) }
Output: application/json
func ContextValue ¶ added in v0.0.6
func ContextValue(key, value interface{}) Option
ContextValue applies a context key/value pair to the *http.Request.
Example ¶
package main import ( "fmt" "github.com/broothie/qst" ) func main() { request, _ := qst.NewGet("https://breakfast.com/api/cereals", qst.ContextValue("frosted", true), ) fmt.Println(request.Context().Value("frosted")) }
Output: true
func Cookie ¶ added in v0.0.5
Cookie applies a cookie to the *http.Request.
Example ¶
package main import ( "fmt" "net/http" "time" "github.com/broothie/qst" ) func main() { request, _ := qst.NewGet("https://breakfast.com/api/cereals", qst.Cookie(&http.Cookie{ Name: "cookie-crisp", Value: "COOOOKIE CRISP!", Path: "/", Expires: time.Now().Add(time.Hour), }), ) fmt.Println(request.Cookie("cookie-crisp")) }
Output: cookie-crisp="COOOOKIE CRISP!" <nil>
func Header ¶
Header applies a key/value pair to the headers of the *http.Request, retaining the existing headers for the key.
Example ¶
package main import ( "fmt" "github.com/broothie/qst" ) func main() { request, _ := qst.NewGet("https://breakfast.com/api/cereals", qst.Header("grain", "oats"), ) fmt.Println(request.Header.Get("grain")) }
Output: oats
func Path ¶ added in v0.1.0
Path joins the segments with path.Join, and appends the result to the *http.Request URL.
Example ¶
package main import ( "fmt" "github.com/broothie/qst" ) func main() { request, _ := qst.NewGet("https://breakfast.com/api/", qst.Path("/cereals", "1234/variants", "frosted"), ) fmt.Println(request.URL.Path) }
Output: /api/cereals/1234/variants/frosted
func Query ¶
Query applies a key/value pair to the query parameters of the *http.Request.
Example ¶
package main import ( "fmt" "github.com/broothie/qst" ) func main() { request, _ := qst.NewGet("https://breakfast.com/api/cereals", qst.Query("page", "10"), ) fmt.Println(request.URL.Query().Encode()) }
Output: page=10
func Referer ¶ added in v0.1.1
Referer applies a "Referer" header to the *http.Request.
Example ¶
package main import ( "fmt" "github.com/broothie/qst" ) func main() { request, _ := qst.NewGet("https://breakfast.com/api/cereals", qst.Referer("https://breakfast.com"), ) fmt.Println(request.Header.Get("Referer")) }
Output: https://breakfast.com
func TokenAuth ¶ added in v0.1.1
TokenAuth applies an "Authorization: Token <token>" header to the *http.Request.
Example ¶
package main import ( "fmt" "github.com/broothie/qst" ) func main() { request, _ := qst.NewGet("https://breakfast.com/api/cereals", qst.TokenAuth("c0rnfl@k3s"), ) fmt.Println(request.Header.Get("Authorization")) }
Output: Token c0rnfl@k3s
func UserAgent ¶ added in v0.1.1
UserAgent applies a "User-Agent" header to the *http.Request.
Example ¶
package main import ( "fmt" "github.com/broothie/qst" ) func main() { request, _ := qst.NewGet("https://breakfast.com/api/cereals", qst.UserAgent("qst"), ) fmt.Println(request.Header.Get("User-Agent")) }
Output: qst
func UserPassword ¶ added in v0.1.0
UserPassword applies the username and password to *http.Request URL User.
Example ¶
package main import ( "fmt" "github.com/broothie/qst" ) func main() { request, _ := qst.NewGet("https://breakfast.com/api/cereals", qst.UserPassword("TonyTheTiger", "grrreat"), ) fmt.Println(request.URL) }
Output: https://TonyTheTiger:grrreat@breakfast.com/api/cereals
func Username ¶ added in v0.1.0
Username applies the username to *http.Request URL User.
Example ¶
package main import ( "fmt" "github.com/broothie/qst" ) func main() { request, _ := qst.NewGet("https://breakfast.com/api/cereals", qst.Username("TonyTheTiger"), ) fmt.Println(request.URL) }
Output: https://TonyTheTiger@breakfast.com/api/cereals
type OptionFunc ¶
OptionFunc is a function form of Option.
Example ¶
package main import ( "fmt" "net/http" "net/http/httptest" "github.com/broothie/qst" ) func main() { token := "c0rnfl@k3s" server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Println(r.Header.Get("Authorization")) })) defer server.Close() client := qst.NewClient(server.Client(), qst.URL(server.URL), qst.OptionFunc(func(request *http.Request) (*http.Request, error) { return qst.BearerAuth(token).Apply(request) }), ) client.Get() token = "c00ki3cr!5p" client.Get() }
Output: Bearer c0rnfl@k3s Bearer c00ki3cr!5p
type Pipeline ¶
type Pipeline []Option
Pipeline is a collection of options, which can be applied as a whole.
type Queries ¶ added in v0.1.0
Queries applies multiple key/value pairs to the query parameters of the *http.Request. It wraps url.Values.
Example ¶
package main import ( "fmt" "github.com/broothie/qst" ) func main() { request, _ := qst.NewGet("https://breakfast.com/api/cereals", qst.Queries{ "page": {"10"}, "limit": {"50"}, }, ) fmt.Println(request.URL.Query().Encode()) }
Output: limit=50&page=10