Documentation
¶
Index ¶
- func Connect(url string, options ...option.Option[*http.Request]) (*http.Response, error)
- func Delete(url string, options ...option.Option[*http.Request]) (*http.Response, error)
- func Do(method, url string, options ...option.Option[*http.Request]) (*http.Response, error)
- func Get(url string, options ...option.Option[*http.Request]) (*http.Response, error)
- func Head(url string, options ...option.Option[*http.Request]) (*http.Response, error)
- func New(method, url string, options ...option.Option[*http.Request]) (*http.Request, error)
- func NewConnect(url string, options ...option.Option[*http.Request]) (*http.Request, error)
- func NewDelete(url string, options ...option.Option[*http.Request]) (*http.Request, error)
- func NewGet(url string, options ...option.Option[*http.Request]) (*http.Request, error)
- func NewHead(url string, options ...option.Option[*http.Request]) (*http.Request, error)
- func NewOptions(url string, options ...option.Option[*http.Request]) (*http.Request, error)
- func NewPatch(url string, options ...option.Option[*http.Request]) (*http.Request, error)
- func NewPost(url string, options ...option.Option[*http.Request]) (*http.Request, error)
- func NewPut(url string, options ...option.Option[*http.Request]) (*http.Request, error)
- func NewTrace(url string, options ...option.Option[*http.Request]) (*http.Request, error)
- func Options(url string, options ...option.Option[*http.Request]) (*http.Response, error)
- func Patch(url string, options ...option.Option[*http.Request]) (*http.Response, error)
- func Post(url string, options ...option.Option[*http.Request]) (*http.Response, error)
- func Put(url string, options ...option.Option[*http.Request]) (*http.Response, error)
- func SetClient(c *http.Client)
- func Trace(url string, options ...option.Option[*http.Request]) (*http.Response, error)
- func WithAcceptHeader(accept string) option.Option[*http.Request]
- func WithAuthorizationHeader(authorization string) option.Option[*http.Request]
- func WithBasicAuth(username, password string) option.Option[*http.Request]
- func WithBearerAuth(token string) option.Option[*http.Request]
- func WithBody(body io.ReadCloser) option.Option[*http.Request]
- func WithBodyBytes(body []byte) option.Option[*http.Request]
- func WithBodyJSON(v interface{}) option.Option[*http.Request]
- func WithBodyReader(body io.Reader) option.Option[*http.Request]
- func WithBodyString(body string) option.Option[*http.Request]
- func WithBodyXML(v interface{}) option.Option[*http.Request]
- func WithContentTypeHeader(contentType string) option.Option[*http.Request]
- func WithContext(ctx context.Context) option.Option[*http.Request]
- func WithContextValue(key, value interface{}) option.Option[*http.Request]
- func WithCookie(cookie *http.Cookie) option.Option[*http.Request]
- func WithDump(w io.Writer) option.Option[*http.Request]
- func WithHeader(key, value string) option.Option[*http.Request]
- func WithHost(host string) option.Option[*http.Request]
- func WithPath(segments ...string) option.Option[*http.Request]
- func WithQuery(key, value string) option.Option[*http.Request]
- func WithRawURL(url *pkgurl.URL) option.Option[*http.Request]
- func WithRefererHeader(referer string) option.Option[*http.Request]
- func WithScheme(scheme string) option.Option[*http.Request]
- func WithTokenAuth(token string) option.Option[*http.Request]
- func WithURL(url string) option.Option[*http.Request]
- func WithUser(user *pkgurl.Userinfo) option.Option[*http.Request]
- func WithUserAgentHeader(userAgent string) option.Option[*http.Request]
- func WithUserPassword(username, password string) option.Option[*http.Request]
- func WithUsername(username string) option.Option[*http.Request]
- type WithBodyForm
- type WithHeaders
- type WithQueries
Examples ¶
- Do
- New
- WithAcceptHeader
- WithAuthorizationHeader
- WithBasicAuth
- WithBearerAuth
- WithBody
- WithBodyBytes
- WithBodyForm
- WithBodyJSON
- WithBodyString
- WithBodyXML
- WithContentTypeHeader
- WithContextValue
- WithCookie
- WithHeader
- WithHeaders
- WithPath
- WithQueries
- WithQuery
- WithRefererHeader
- WithTokenAuth
- WithUserAgentHeader
- WithUserPassword
- WithUsername
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Do ¶
Do makes an *http.Request using the global client 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.WithPath("api", "/cereals", "1234"),
qst.WithBearerAuth("c0rnfl@k3s"),
qst.WithBodyJSON(map[string]string{"name": "Honey Bunches of Oats"}),
)
}
Output: POST /api/cereals/1234 Bearer c0rnfl@k3s {"name":"Honey Bunches of Oats"}
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.WithScheme("https"),
qst.WithHost("breakfast.com"),
qst.WithPath("/cereals", "1234"),
qst.WithBearerAuth("c0rnfl@k3s"),
qst.WithBodyJSON(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 WithAcceptHeader ¶ added in v0.1.2
WithAcceptHeader 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.WithAcceptHeader("application/json"),
)
fmt.Println(request.Header.Get("Accept"))
}
Output: application/json
func WithAuthorizationHeader ¶ added in v0.1.2
WithAuthorizationHeader 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.WithAuthorizationHeader("c0rnfl@k3s"),
)
fmt.Println(request.Header.Get("Authorization"))
}
Output: c0rnfl@k3s
func WithBasicAuth ¶ added in v0.1.2
WithBasicAuth 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.WithBasicAuth("TonyTheTiger", "grrreat"),
)
fmt.Println(request.Header.Get("Authorization"))
}
Output: Basic VG9ueVRoZVRpZ2VyOmdycnJlYXQ=
func WithBearerAuth ¶ added in v0.1.2
WithBearerAuth 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.WithBearerAuth("c0rnfl@k3s"),
)
fmt.Println(request.Header.Get("Authorization"))
}
Output: Bearer c0rnfl@k3s
func WithBody ¶ added in v0.1.2
WithBody 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.WithBody(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 WithBodyBytes ¶ added in v0.1.2
WithBodyBytes 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.WithBodyBytes([]byte("Part of a complete breakfast.")),
)
body, _ := ioutil.ReadAll(request.Body)
fmt.Println(string(body))
}
Output: Part of a complete breakfast.
func WithBodyJSON ¶ added in v0.1.2
WithBodyJSON 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.WithBodyJSON(map[string]string{"name": "Rice Krispies"}),
)
body, _ := ioutil.ReadAll(request.Body)
fmt.Println(string(body))
}
Output: {"name":"Rice Krispies"}
func WithBodyReader ¶ added in v0.1.2
WithBodyReader applies an io.Reader to the *http.Request body.
func WithBodyString ¶ added in v0.1.2
WithBodyString 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.WithBodyString("Part of a complete breakfast."),
)
body, _ := ioutil.ReadAll(request.Body)
fmt.Println(string(body))
}
Output: Part of a complete breakfast.
func WithBodyXML ¶ added in v0.1.2
WithBodyXML 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.WithBodyXML("Part of a complete breakfast."),
)
body, _ := ioutil.ReadAll(request.Body)
fmt.Println(string(body))
}
Output: <string>Part of a complete breakfast.</string>
func WithContentTypeHeader ¶ added in v0.1.2
WithContentTypeHeader 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.WithContentTypeHeader("application/json"),
)
fmt.Println(request.Header.Get("Content-Type"))
}
Output: application/json
func WithContext ¶ added in v0.1.2
WithContext applies a context.Context to the *http.Request.
func WithContextValue ¶ added in v0.1.2
WithContextValue 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.WithContextValue("frosted", true),
)
fmt.Println(request.Context().Value("frosted"))
}
Output: true
func WithCookie ¶ added in v0.1.2
WithCookie 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.WithCookie(&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 WithHeader ¶ added in v0.1.2
WithHeader 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.WithHeader("grain", "oats"),
)
fmt.Println(request.Header.Get("grain"))
}
Output: oats
func WithHost ¶ added in v0.1.2
WithHost applies the host to the *http.Request and *http.Request URL.
func WithPath ¶ added in v0.1.2
WithPath 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.WithPath("/cereals", "1234/variants", "frosted"),
)
fmt.Println(request.URL.Path)
}
Output: /api/cereals/1234/variants/frosted
func WithQuery ¶ added in v0.1.2
WithQuery 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.WithQuery("page", "10"),
)
fmt.Println(request.URL.Query().Encode())
}
Output: page=10
func WithRawURL ¶ added in v0.1.2
WithRawURL applies the URL to the *http.Request.
func WithRefererHeader ¶ added in v0.1.2
WithRefererHeader 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.WithRefererHeader("https://breakfast.com"),
)
fmt.Println(request.Header.Get("Referer"))
}
Output: https://breakfast.com
func WithScheme ¶ added in v0.1.2
WithScheme applies the scheme to the *http.Request URL.
func WithTokenAuth ¶ added in v0.1.2
WithTokenAuth 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.WithTokenAuth("c0rnfl@k3s"),
)
fmt.Println(request.Header.Get("Authorization"))
}
Output: Token c0rnfl@k3s
func WithUserAgentHeader ¶ added in v0.1.2
WithUserAgentHeader 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.WithUserAgentHeader("qst"),
)
fmt.Println(request.Header.Get("User-Agent"))
}
Output: qst
func WithUserPassword ¶ added in v0.1.2
WithUserPassword 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.WithUserPassword("TonyTheTiger", "grrreat"),
)
fmt.Println(request.URL)
}
Output: https://TonyTheTiger:[email protected]/api/cereals
func WithUsername ¶ added in v0.1.2
WithUsername 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.WithUsername("TonyTheTiger"),
)
fmt.Println(request.URL)
}
Output: https://[email protected]/api/cereals
Types ¶
type WithBodyForm ¶ added in v0.1.2
WithBodyForm 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.WithBodyForm{"name": {"Grape Nuts"}},
)
body, _ := ioutil.ReadAll(request.Body)
fmt.Println(string(body))
}
Output: name=Grape+Nuts
type WithHeaders ¶ added in v0.1.2
WithHeaders 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.WithHeaders{
"grain": {"oats"},
"style": {"toasted"},
},
)
fmt.Println(request.Header)
}
Output: map[Grain:[oats] Style:[toasted]]
type WithQueries ¶ added in v0.1.2
WithQueries 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.WithQueries{
"page": {"10"},
"limit": {"50"},
},
)
fmt.Println(request.URL.Query().Encode())
}
Output: limit=50&page=10