Documentation ¶
Overview ¶
Example ¶
package main import ( "time" "github.com/DavidCai1993/request" ) var ( err error res *request.Response json interface{} ) func main() { res, err = request. Get("http://mysite.com"). End() // json has the type *simplejson.Json json, err = request. Post("http://mysite.com"). Timeout(30*time.Second). Send(map[string]string{"name": "David"}). Set("X-HEADER-KEY", "foo"). Accept("application/json"). JSON() }
Output:
Index ¶
- Constants
- Variables
- func GetIndex(v interface{}, index int) interface{}
- func GetPath(v interface{}, branch ...string) interface{}
- type Client
- func (c *Client) Accept(t string) *Client
- func (c *Client) Add(key, value string) *Client
- func (c *Client) Attach(fieldname, path, filename string) *Client
- func (c *Client) Auth(name, password string) *Client
- func (c *Client) Cookie(cookie *http.Cookie) *Client
- func (c *Client) CookieJar(jar http.CookieJar) *Client
- func (c *Client) Delete(URL string) *Client
- func (c *Client) End() (*Response, error)
- func (c *Client) Field(vals url.Values) *Client
- func (c *Client) Get(URL string) *Client
- func (c *Client) GetResponse() *Response
- func (c *Client) Header(h http.Header) *Client
- func (c *Client) JSON(v ...interface{}) (interface{}, error)
- func (c *Client) Post(URL string) *Client
- func (c *Client) Proxy(addr string) *Client
- func (c *Client) Put(URL string) *Client
- func (c *Client) Query(vals url.Values) *Client
- func (c *Client) Redirects(count int) *Client
- func (c *Client) Req() (*http.Request, error)
- func (c *Client) ResponseCode() int
- func (c *Client) ResponseData() string
- func (c *Client) ResponseDataItem(item string) interface{}
- func (c *Client) ResponseDataItemBool(item string) bool
- func (c *Client) ResponseDataItemFloat(item string) float64
- func (c *Client) ResponseDataItemInt(item string) int
- func (c *Client) ResponseDataItemInt64(item string) int64
- func (c *Client) ResponseDataItemString(item string) string
- func (c *Client) ResponseDataItemTime(item string) time.Time
- func (c *Client) ResponseOK() bool
- func (c *Client) ResponseReason() string
- func (c *Client) ResponseURL() string
- func (c *Client) Send(body interface{}) *Client
- func (c *Client) Set(key, value string) *Client
- func (c *Client) Text() (string, error)
- func (c *Client) Timeout(timeout time.Duration) *Client
- func (c *Client) To(method string, URL string) *Client
- func (c *Client) Type(t string) *Client
- type Response
- func (r *Response) Code() int
- func (r *Response) Content() ([]byte, error)
- func (r *Response) GetPath(v interface{}, branch ...string) interface{}
- func (r *Response) GetURL() string
- func (r *Response) JSON(v ...interface{}) (interface{}, error)
- func (r *Response) OK() bool
- func (r *Response) RAWJSON(v ...interface{}) (interface{}, error)
- func (r *Response) Raw() ([]byte, error)
- func (r *Response) Reason() string
- func (r *Response) Text() (string, error)
- func (r *Response) URL() (*url.URL, error)
Examples ¶
Constants ¶
const Version = "1.6.0"
Version is this package's version number.
Variables ¶
var ( ErrNotPOST = errors.New("request: method is not POST when using form") ErrLackURL = errors.New("request: request lacks URL") ErrLackMethod = errors.New("request: request lacks method") ErrBodyAlreadySet = errors.New("request: request body has already been set") ErrStatusNotOk = errors.New("request: status code is not ok (>= 400)") )
Errors used by this package.
Functions ¶
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a HTTP client which provides usable and chainable methods.
func Get ¶
Get equals New().Get(URL) to let you start a GET request conveniently.
Example ¶
package main import ( "github.com/DavidCai1993/request" ) var ( err error json interface{} ) func main() { json, err = request. Get("http://mysite.com"). JSON() }
Output:
func Post ¶
Post equals New().Post(URL) to let you start a POST request conveniently.
Example ¶
package main import ( "github.com/DavidCai1993/request" ) var ( err error json interface{} ) func main() { json, err = request. Post("http://mysite.com"). Send(map[string]string{"name": "David"}). Set("X-HEADER-KEY", "foo"). Accept("application/json"). JSON() }
Output:
func (*Client) Accept ¶
Accept sets the "Accept" request header to the given value. Some shorthands are supported:
"html": "text/html" "json": "application/json" "xml": "application/xml" "text": "text/plain" "urlencoded": "application/x-www-form-urlencoded" "form": "application/x-www-form-urlencoded" "form-data": "application/x-www-form-urlencoded" "multipart": "multipart/form-data"
So you can just call .Accept("json") to set the "Accept" header to "application/json".
func (*Client) Add ¶
Add adds the key, value pair to the request header.It appends to any existing values associated with key.
func (*Client) Attach ¶
Attach adds the attachment file to the form. Once the attachment was set, the "Content-Type" will be set to "multipart/form-data; boundary=xxx" automatically.
Example ¶
package main import ( "net/url" "github.com/DavidCai1993/request" ) var ( err error json interface{} ) func main() { json, err = request. Post("http://mysite.com/readme"). Field(url.Values{"key": []string{"value1"}}). Attach("test.md", "./README.md", "README.md"). JSON() }
Output:
func (*Client) Auth ¶
Auth sets the request's Authorization header to use HTTP Basic Authentication with the provided username and password.
With HTTP Basic Authentication the provided username and password are not encrypted.
Example ¶
package main import ( "github.com/DavidCai1993/request" ) var ( err error json interface{} ) func main() { json, err = request. Get("http://mysite.com/somebooks"). Auth("name", "passwd"). JSON() }
Output:
func (*Client) Cookie ¶
Cookie adds the cookie to the request.
Example ¶
package main import ( "net/http" "github.com/DavidCai1993/request" ) var ( text string err error ) func main() { text, err = request. Get("http://mysite.com/get"). Cookie(&http.Cookie{Name: "name", Value: "David"}). Text() }
Output:
func (*Client) End ¶
End sends the HTTP request and returns the HTTP reponse.
An error is returned if caused by client policy (such as timeout), or failure to speak HTTP (such as a network connectivity problem), or generated by former chained methods. A non-2xx status code doesn't cause an error.
func (*Client) Field ¶
Field sets the field values like form fields in HTML. Once it was set, the "Content-Type" header of the request will be automatically set to "application/x-www-form-urlencoded".
func (*Client) GetResponse ¶
func (*Client) Header ¶
Header sets all key, value pairs in h to the request header, it replaces any existing values associated with key.
func (*Client) Proxy ¶
Proxy sets the address of the proxy which used by the request.
Example ¶
package main import ( "github.com/DavidCai1993/request" ) var ( err error json interface{} ) func main() { json, err = request. Get("http://mysite.com/somebooks"). Proxy("http://myproxy.com:8080"). JSON() }
Output:
func (*Client) Redirects ¶
Redirects sets the max redirects count for the request. If not set, request will use its default policy, which is to stop after 10 consecutive requests.
func (*Client) Req ¶
Req returns the representing http.Request instance of this request. It is often used in wirting tests.
func (*Client) ResponseData ¶
func (*Client) ResponseDataItem ¶
func (*Client) ResponseDataItemBool ¶
func (*Client) ResponseDataItemFloat ¶
func (*Client) ResponseDataItemInt ¶
func (*Client) ResponseDataItemInt64 ¶
func (*Client) ResponseDataItemString ¶
func (*Client) ResponseOK ¶
func (*Client) ResponseReason ¶
func (*Client) ResponseURL ¶
func (*Client) Send ¶
Send sends the body in JSON format, body can be anything which can be Marshaled or just Marshaled JSON string.
func (*Client) Set ¶
Set sets the request header entries associated with key to the single element value. It replaces any existing values associated with key.
func (*Client) Timeout ¶
Timeout specifies a time limit for the request. The timeout includes connection time, any redirects, and reading the response body. The timer remains running after Get, Head, Post, or End return and will interrupt reading of the response body.
func (*Client) Type ¶
Type sets the "Content-Type" request header to the given value. Some shorthands are supported:
"html": "text/html" "json": "application/json" "xml": "application/xml" "text": "text/plain" "urlencoded": "application/x-www-form-urlencoded" "form": "application/x-www-form-urlencoded" "form-data": "application/x-www-form-urlencoded" "multipart": "multipart/form-data"
So you can just call .Type("html") to set the "Content-Type" header to "text/html".
type Response ¶
Response represents the response from a HTTP request.
func (*Response) Content ¶
Content returns the content of the response body, it will handle the compression.