napping: github.com/jmcvetta/napping Index | Files | Directories

package napping

import "github.com/jmcvetta/napping"

Package napping is a client library for interacting with RESTful APIs.

Example:

type Foo struct {
	Bar string
}
type Spam struct {
	Eggs int
}
payload := Foo{
	Bar: "baz",
}
result := Spam{}
url := "http://foo.com/bar"
resp, err := napping.Post(url, &payload, &result, nil)
if err != nil {
	panic(err)
}
if resp.Status() == 200 {
	println(result.Eggs)
}

See the "examples" folder for a more complete example.

Index

Package Files

api.go doc.go request.go session.go util.go

type Params Uses

type Params map[string]string

A Params is a map containing URL parameters.

func (Params) AsUrlValues Uses

func (p Params) AsUrlValues() url.Values

AsUrlValues converts Params to url.Values

type Request Uses

type Request struct {
    Url     string      // Raw URL string
    Method  string      // HTTP method to use
    Params  *url.Values // URL query parameters
    Payload interface{} // Data to JSON-encode and POST

    // Can be set to true if Payload is of type *bytes.Buffer and client wants
    // to send it as-is
    RawPayload bool

    // Result is a pointer to a data structure.  On success (HTTP status < 300),
    // response from server is unmarshaled into Result.
    Result interface{}

    // CaptureResponseBody can be set to capture the response body for external use.
    CaptureResponseBody bool

    // ResponseBody exports the raw response body if CaptureResponseBody is true.
    ResponseBody *bytes.Buffer

    // Error is a pointer to a data structure.  On error (HTTP status >= 300),
    // response from server is unmarshaled into Error.
    Error interface{}

    // Optional
    Userinfo *url.Userinfo
    Header   *http.Header

    // Custom Transport if needed.
    Transport *http.Transport
    // contains filtered or unexported fields
}

A Request describes an HTTP request to be executed, data structures into which the result will be unmarshaled, and the server's response. By using a single object for both the request and the response we allow easy access to Result and Error objects without needing type assertions.

type Response Uses

type Response Request

A Response is a Request object that has been executed.

func Delete Uses

func Delete(url string, p *url.Values, result, errMsg interface{}) (*Response, error)

Delete sends a DELETE request.

func Get Uses

func Get(url string, p *url.Values, result, errMsg interface{}) (*Response, error)

Get sends a GET request.

func Head(url string, result, errMsg interface{}) (*Response, error)

Head sends a HEAD request.

func Options Uses

func Options(url string, result, errMsg interface{}) (*Response, error)

Options sends an OPTIONS request.

func Patch Uses

func Patch(url string, payload, result, errMsg interface{}) (*Response, error)

Patch sends a PATCH request.

func Post Uses

func Post(url string, payload, result, errMsg interface{}) (*Response, error)

Post sends a POST request.

func Put Uses

func Put(url string, payload, result, errMsg interface{}) (*Response, error)

Put sends a PUT request.

func Send Uses

func Send(r *Request) (*Response, error)

Send composes and sends and HTTP request.

func (*Response) HttpResponse Uses

func (r *Response) HttpResponse() *http.Response

HttpResponse returns the underlying Response object from http package.

func (*Response) RawText Uses

func (r *Response) RawText() string

RawText returns the body of the server's response as raw text.

func (*Response) Status Uses

func (r *Response) Status() int

Status returns the HTTP status for the executed request, or 0 if request has not yet been sent.

func (*Response) Timestamp Uses

func (r *Response) Timestamp() time.Time

Timestamp returns the time when HTTP request was sent.

func (*Response) Unmarshal Uses

func (r *Response) Unmarshal(v interface{}) error

Unmarshal parses the JSON-encoded data in the server's response, and stores the result in the value pointed to by v.

type Session Uses

type Session struct {
    Client *http.Client
    Log    bool // Log request and response

    // Optional
    Userinfo *url.Userinfo

    // Optional defaults - can be overridden in a Request
    Header *http.Header
    Params *url.Values
}

Session defines the napping session structure

func (*Session) Delete Uses

func (s *Session) Delete(url string, p *url.Values, result, errMsg interface{}) (*Response, error)

Delete sends a DELETE request.

func (*Session) Get Uses

func (s *Session) Get(url string, p *url.Values, result, errMsg interface{}) (*Response, error)

Get sends a GET request.

func (*Session) Head Uses

func (s *Session) Head(url string, result, errMsg interface{}) (*Response, error)

Head sends a HEAD request.

func (*Session) Options Uses

func (s *Session) Options(url string, result, errMsg interface{}) (*Response, error)

Options sends an OPTIONS request.

func (*Session) Patch Uses

func (s *Session) Patch(url string, payload, result, errMsg interface{}) (*Response, error)

Patch sends a PATCH request.

func (*Session) Post Uses

func (s *Session) Post(url string, payload, result, errMsg interface{}) (*Response, error)

Post sends a POST request.

func (*Session) Put Uses

func (s *Session) Put(url string, payload, result, errMsg interface{}) (*Response, error)

Put sends a PUT request.

func (*Session) Send Uses

func (s *Session) Send(r *Request) (response *Response, err error)

Send constructs and sends an HTTP request.

Directories

PathSynopsis
examples/github_auth_tokenExample demonstrating use of package napping, with HTTP Basic authentictation over HTTPS, to retrieve a Github auth token.
examples/httpbin

Package napping imports 10 packages (graph) and is imported by 78 packages. Updated 2017-03-11. Refresh now. Tools for package owners.