Documentation
¶
Overview ¶
Package requests is a convenience wrapper around net/http to make it faster and easier to build requests and custom transports.
Example ¶
package main
import (
"context"
"fmt"
"strings"
"github.com/carlmjohnson/requests"
)
func main() {
// Simple GET into a string
var s string
err := requests.
URL("http://example.com").
ToString(&s).
Fetch(context.Background())
if err != nil {
fmt.Println("could not connect to example.com:", err)
}
fmt.Println(strings.Contains(s, "Example Domain"))
}
Output: true
Example (Bufio) ¶
package main
import (
"bufio"
"context"
"fmt"
"io"
"strings"
"github.com/carlmjohnson/requests"
)
func main() {
// read a response line by line for a sentinel
found := false
err := requests.
URL("http://example.com").
ToBufioReader(func(r *bufio.Reader) error {
var err error
for s := ""; err == nil; {
if strings.Contains(s, "Example Domain") {
found = true
return nil
}
// read one line from response
s, err = r.ReadString('\n')
}
if err == io.EOF {
return nil
}
return err
}).
Fetch(context.Background())
if err != nil {
fmt.Println("could not connect to example.com:", err)
}
fmt.Println(found)
}
Output: true
Example (BytesBuffer) ¶
package main
import (
"bytes"
"context"
"fmt"
"strings"
"github.com/carlmjohnson/requests"
)
func main() {
// Simple GET into a buffer
var buf bytes.Buffer
err := requests.
URL("http://example.com").
ToBytesBuffer(&buf).
Fetch(context.Background())
if err != nil {
fmt.Println("could not connect to example.com:", err)
}
fmt.Println(strings.Contains(buf.String(), "Example Domain"))
}
Output: true
Example (ExpectStatus) ¶
package main
import (
"context"
"fmt"
"github.com/carlmjohnson/requests"
)
func main() {
// Expect a specific status code
err := requests.
URL("https://jsonplaceholder.typicode.com/posts/9001").
AddValidator(requests.CheckStatus(404)).
AddValidator(requests.MatchContentType("application/json")).
Fetch(context.Background())
if err != nil {
fmt.Println("should be a 404:", err)
} else {
fmt.Println("OK")
}
}
Output: OK
Example (FormValue) ¶
package main
import (
"context"
"fmt"
"net/url"
"github.com/carlmjohnson/requests"
)
// Examples with the Postman echo server
type postman struct {
Args map[string]string `json:"args"`
Data string `json:"data"`
Headers map[string]string `json:"headers"`
JSON map[string]string `json:"json"`
}
func main() {
// Submit form values
var echo postman
err := requests.
URL("https://postman-echo.com/put").
Put().
BodyForm(url.Values{
"hello": []string{"world"},
}).
ToJSON(&echo).
Fetch(context.Background())
if err != nil {
fmt.Println("problem with postman:", err)
}
fmt.Println(echo.JSON)
}
Output: map[hello:world]
Example (GetJSON) ¶
package main
import (
"context"
"fmt"
"github.com/carlmjohnson/requests"
)
type placeholder struct {
ID int `json:"id,omitempty"`
Title string `json:"title"`
Body string `json:"body"`
UserID int `json:"userId"`
}
func main() {
// GET a JSON object
var post placeholder
err := requests.
URL("https://jsonplaceholder.typicode.com").
Path("/posts/1").
ToJSON(&post).
Fetch(context.Background())
if err != nil {
fmt.Println("could not connect to jsonplaceholder.typicode.com:", err)
}
fmt.Println(post.Title)
}
Output: sunt aut facere repellat provident occaecati excepturi optio reprehenderit
Example (Header) ¶
package main
import (
"context"
"fmt"
"github.com/carlmjohnson/requests"
)
// Examples with the Postman echo server
type postman struct {
Args map[string]string `json:"args"`
Data string `json:"data"`
Headers map[string]string `json:"headers"`
JSON map[string]string `json:"json"`
}
func main() {
// Set headers
var headers postman
err := requests.
URL("https://postman-echo.com/get").
UserAgent("bond/james-bond").
ContentType("secret").
Header("martini", "shaken").
ToJSON(&headers).
Fetch(context.Background())
if err != nil {
fmt.Println("problem with postman:", err)
}
fmt.Println(headers.Headers["user-agent"])
fmt.Println(headers.Headers["content-type"])
fmt.Println(headers.Headers["martini"])
}
Output: bond/james-bond secret shaken
Example (PostJSON) ¶
package main
import (
"context"
"fmt"
"github.com/carlmjohnson/requests"
)
type placeholder struct {
ID int `json:"id,omitempty"`
Title string `json:"title"`
Body string `json:"body"`
UserID int `json:"userId"`
}
func main() {
// POST a JSON object and parse the response
var res placeholder
req := placeholder{
Title: "foo",
Body: "baz",
UserID: 1,
}
err := requests.
URL("/posts").
Host("jsonplaceholder.typicode.com").
BodyJSON(&req).
ToJSON(&res).
Fetch(context.Background())
if err != nil {
fmt.Println("could not connect to jsonplaceholder.typicode.com:", err)
}
fmt.Println(res)
}
Output: {101 foo baz 1}
Example (QueryParam) ¶
package main
import (
"context"
"fmt"
"github.com/carlmjohnson/requests"
)
// Examples with the Postman echo server
type postman struct {
Args map[string]string `json:"args"`
Data string `json:"data"`
Headers map[string]string `json:"headers"`
JSON map[string]string `json:"json"`
}
func main() {
// Set a query parameter
var params postman
err := requests.
URL("https://postman-echo.com/get?a=1&b=2").
Param("b", "3").
Param("c", "4").
ToJSON(¶ms).
Fetch(context.Background())
if err != nil {
fmt.Println("problem with postman:", err)
}
fmt.Println(params.Args)
}
Output: map[a:1 b:3 c:4]
Example (RawBody) ¶
package main
import (
"context"
"fmt"
"github.com/carlmjohnson/requests"
)
// Examples with the Postman echo server
type postman struct {
Args map[string]string `json:"args"`
Data string `json:"data"`
Headers map[string]string `json:"headers"`
JSON map[string]string `json:"json"`
}
func main() {
// Post a raw body
var data postman
err := requests.
URL("https://postman-echo.com/post").
BodyBytes([]byte(`hello, world`)).
ContentType("text/plain").
ToJSON(&data).
Fetch(context.Background())
if err != nil {
fmt.Println("problem with postman:", err)
}
fmt.Println(data.Data)
}
Output: hello, world
Index ¶
- func HasStatusErr(err error, codes ...int) bool
- func WrapRoundTripper(rt http.RoundTripper, f func(r *http.Request)) http.RoundTripper
- func WrapTransport(c *http.Client, f func(r *http.Request))
- type BodyGetter
- type Builder
- func (rb *Builder) AddValidator(h ResponseHandler) *Builder
- func (rb *Builder) BasicAuth(username, password string) *Builder
- func (rb *Builder) BodyBytes(b []byte) *Builder
- func (rb *Builder) BodyForm(data url.Values) *Builder
- func (rb *Builder) BodyJSON(v interface{}) *Builder
- func (rb *Builder) BodyReader(r io.Reader) *Builder
- func (rb *Builder) CheckStatus(acceptStatuses ...int) *Builder
- func (rb *Builder) Client(cl *http.Client) *Builder
- func (rb *Builder) Clone() *Builder
- func (rb *Builder) ContentType(ct string) *Builder
- func (rb *Builder) Do(req *http.Request) (err error)
- func (rb *Builder) Fetch(ctx context.Context) (err error)
- func (rb *Builder) Get() *Builder
- func (rb *Builder) GetBody(src BodyGetter) *Builder
- func (rb *Builder) Handle(h ResponseHandler) *Builder
- func (rb *Builder) Header(key, value string) *Builder
- func (rb *Builder) Host(host string) *Builder
- func (rb *Builder) Method(method string) *Builder
- func (rb *Builder) Param(key, value string) *Builder
- func (rb *Builder) Path(path string) *Builder
- func (rb *Builder) Post() *Builder
- func (rb *Builder) Put() *Builder
- func (rb *Builder) Request(ctx context.Context) (req *http.Request, err error)
- func (rb *Builder) ToBufioReader(f func(r *bufio.Reader) error) *Builder
- func (rb *Builder) ToBytesBuffer(buf *bytes.Buffer) *Builder
- func (rb *Builder) ToJSON(v interface{}) *Builder
- func (rb *Builder) ToString(sp *string) *Builder
- func (rb *Builder) UserAgent(s string) *Builder
- type ResponseHandler
- func ChainHandlers(handlers ...ResponseHandler) ResponseHandler
- func CheckStatus(acceptStatuses ...int) ResponseHandler
- func MatchContentType(ct string) ResponseHandler
- func ToBufioReader(f func(r *bufio.Reader) error) ResponseHandler
- func ToBytesBuffer(buf *bytes.Buffer) ResponseHandler
- func ToJSON(v interface{}) ResponseHandler
- func ToString(sp *string) ResponseHandler
- type StatusError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HasStatusErr ¶
HasStatusErr returns true if err is a StatusError caused by any of the codes given.
Example ¶
package main
import (
"context"
"fmt"
"github.com/carlmjohnson/requests"
)
func main() {
err := requests.
URL("http://example.com/404").
CheckStatus(200).
Fetch(context.Background())
if requests.HasStatusErr(err, 404) {
fmt.Println("got a 404")
}
}
Output: got a 404
func WrapRoundTripper ¶
func WrapRoundTripper(rt http.RoundTripper, f func(r *http.Request)) http.RoundTripper
WrapRoundTripper deep clones a request and passes it to f before calling the underlying RoundTripper. If rt is nil, it calls http.DefaultTransport.
Types ¶
type BodyGetter ¶
type BodyGetter = func() (io.ReadCloser, error)
BodyGetter provides a Builder with a source for a request body.
func BodyBytes ¶
func BodyBytes(b []byte) BodyGetter
BodyBytes is a BodyGetter that returns the provided raw bytes.
func BodyForm ¶
func BodyForm(data url.Values) BodyGetter
BodyForm is a BodyGetter that builds an encoded form body.
func BodyJSON ¶
func BodyJSON(v interface{}) BodyGetter
BodyJSON is a BodyGetter that marshals a JSON object.
func BodyReader ¶
func BodyReader(r io.Reader) BodyGetter
BodyReader is a BodyGetter that returns an io.Reader.
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder is a convenient way to build, send, and handle HTTP requests. A configured Builder can be used for concurrent fetching. The zero value of Builder is usable but the Host must be set before fetching.
func (*Builder) AddValidator ¶
func (rb *Builder) AddValidator(h ResponseHandler) *Builder
AddValidator adds a response validator to the Builder. Adding a validator disables DefaultValidator. To disable all validation, just add nil.
func (*Builder) BodyForm ¶
BodyForm sets the Builder's request body to the encoded form. It also sets the ContentType to "application/x-www-form-urlencoded".
func (*Builder) BodyJSON ¶
BodyJSON sets the Builder's request body to the marshaled JSON. It also sets ContentType to "application/json".
func (*Builder) BodyReader ¶
BodyReader sets the Builder's request body to r.
func (*Builder) CheckStatus ¶
CheckStatus adds a validator for status code of a response.
func (*Builder) Client ¶
Client sets the http.Client to use for requests. If nil, it uses http.DefaultClient.
func (*Builder) ContentType ¶
ContentType sets the Content-Type header.
func (*Builder) Do ¶
Do calls the underlying http.Client and validates and handles any resulting response. The response body is closed after all validators and the handler run.
func (*Builder) GetBody ¶
func (rb *Builder) GetBody(src BodyGetter) *Builder
GetBody sets the BodySource for a request. It implicitly sets method to POST.
func (*Builder) Handle ¶
func (rb *Builder) Handle(h ResponseHandler) *Builder
Handle sets the response handler for a Builder. To use multiple handlers, use ChainHandlers.
func (*Builder) Header ¶
Header sets a header on a request. It overwrites the value of existing keys.
func (*Builder) Param ¶
Param sets a query parameter on a request. It overwrites the value of existing keys.
func (*Builder) ToBufioReader ¶
ToBufioReader sets the Builder to call a callback with the response body wrapped in a bufio.Reader.
func (*Builder) ToBytesBuffer ¶
ToBytesBuffer sets the Builder to write the response body to the provided bytes.Buffer.
type ResponseHandler ¶
ResponseHandler is used to validate or handle the response to a request.
var DefaultValidator ResponseHandler = CheckStatus( http.StatusOK, http.StatusCreated, http.StatusAccepted, http.StatusNonAuthoritativeInfo, http.StatusNoContent, )
DefaultValidator is the validator applied by Builder unless otherwise specified.
func ChainHandlers ¶
func ChainHandlers(handlers ...ResponseHandler) ResponseHandler
ChainHandlers allows for the composing of validators or response handlers.
func CheckStatus ¶
func CheckStatus(acceptStatuses ...int) ResponseHandler
CheckStatus validates the response has an acceptable status code.
func MatchContentType ¶
func MatchContentType(ct string) ResponseHandler
MatchContentType validates that a response has the given content type.
func ToBufioReader ¶
func ToBufioReader(f func(r *bufio.Reader) error) ResponseHandler
ToBufioReader takes a callback which wraps the response body in a bufio.Reader.
func ToBytesBuffer ¶
func ToBytesBuffer(buf *bytes.Buffer) ResponseHandler
ToBytesBuffer writes the response body to the provided bytes.Buffer.
func ToJSON ¶
func ToJSON(v interface{}) ResponseHandler
ToJSON decodes a response as a JSON object.
func ToString ¶
func ToString(sp *string) ResponseHandler
ToString writes the response body to the provided string pointer.
type StatusError ¶
StatusError is the error type produced by CheckStatus.