Documentation ¶
Index ¶
- Constants
- Variables
- func GetDefaultClient() *http.Client
- func GetDefaultDialer() *net.Dialer
- func GetDefaultTransport() *http.Transport
- func GetHostDelay(host string) time.Duration
- func IsDebug() bool
- func MakeClient(transport http.RoundTripper, jar http.CookieJar) *http.Client
- func MakeCookiejar() http.CookieJar
- func MakeTransport(ip string) *http.Transport
- func ResetCookie(urlstr string) error
- func SetDebug(d bool)
- func SetHostDelay(host string, delay time.Duration)
- func SetOption(option *Option)
- type ClientGetter
- type File
- type HttpAgent
- func (s *HttpAgent) AddCookie(c *http.Cookie) *HttpAgent
- func (s *HttpAgent) Bytes(status ...int) ([]byte, int, error)
- func (s *HttpAgent) ClearAgent()
- func (s *HttpAgent) Delete(targetUrl string) *HttpAgent
- func (s *HttpAgent) End(callback ...func(response *http.Response, errs []error)) (*http.Response, []error)
- func (s *HttpAgent) Get(targetUrl string) *HttpAgent
- func (s *HttpAgent) Head(targetUrl string) *HttpAgent
- func (s *HttpAgent) Jar(use bool) *HttpAgent
- func (s *HttpAgent) MaxRedirect(redirect int) *HttpAgent
- func (s *HttpAgent) Param(key string, value string) *HttpAgent
- func (s *HttpAgent) Patch(targetUrl string) *HttpAgent
- func (s *HttpAgent) Post(targetUrl string) *HttpAgent
- func (s *HttpAgent) Proxy(proxyUrl string) *HttpAgent
- func (s *HttpAgent) Put(targetUrl string) *HttpAgent
- func (s *HttpAgent) Query(content interface{}) *HttpAgent
- func (s *HttpAgent) Send(content interface{}) *HttpAgent
- func (s *HttpAgent) SendBytes(data []byte) *HttpAgent
- func (s *HttpAgent) SendFile(file interface{}, args ...string) *HttpAgent
- func (s *HttpAgent) SendParam(key string, value interface{}) *HttpAgent
- func (s *HttpAgent) SendString(content string) *HttpAgent
- func (s *HttpAgent) Set(param string, value string) *HttpAgent
- func (s *HttpAgent) String(status ...int) (string, int, error)
- func (s *HttpAgent) TLSClientConfig(config *tls.Config) *HttpAgent
- func (s *HttpAgent) Timeout(timeout time.Duration) *HttpAgent
- func (s *HttpAgent) ToJSON(v interface{}, status ...int) (int, error)
- func (s *HttpAgent) ToXML(v interface{}, status ...int) (int, error)
- func (s *HttpAgent) Type(typeStr string) *HttpAgent
- type IpRollClient
- type MultipartStreamer
- func (m *MultipartStreamer) Boundary() string
- func (m *MultipartStreamer) GetReader() io.ReadCloser
- func (m *MultipartStreamer) Len() int64
- func (m *MultipartStreamer) SetupRequest(req *http.Request)
- func (m *MultipartStreamer) WriteFields(fields url.Values) error
- func (m *MultipartStreamer) WriteFile(key, filename string) error
- func (m *MultipartStreamer) WriteReader(f File) (err error)
- type Option
Constants ¶
const ( POST = "POST" GET = "GET" HEAD = "HEAD" PUT = "PUT" DELETE = "DELETE" PATCH = "PATCH" )
HTTP methods we support
Variables ¶
var Types = map[string]string{
"html": "text/html",
"json": "application/json",
"xml": "application/xml",
"urlencoded": "application/x-www-form-urlencoded",
"form": "application/x-www-form-urlencoded",
"form-data": "application/x-www-form-urlencoded",
"text": "text/plain",
"multipart": "multipart/form-data",
"stream": "application/octet-stream",
}
Functions ¶
func GetDefaultClient ¶
func GetDefaultDialer ¶
func GetDefaultTransport ¶
func GetHostDelay ¶
func MakeClient ¶
func MakeCookiejar ¶
func MakeTransport ¶
func ResetCookie ¶
func SetHostDelay ¶
Types ¶
type ClientGetter ¶
type ClientGetter interface {
GetHttpClient(httpurl string, proxyurl string, usejar bool) (*http.Client, error)
}
func GetDefaultGetter ¶
func GetDefaultGetter() ClientGetter
type HttpAgent ¶
type HttpAgent struct { Url string ProxyUrl string Method string Header map[string]string TargetType string ForceType string Data map[string]interface{} FormData url.Values QueryData url.Values FileData []File Cookies []*http.Cookie TlsConfig *tls.Config MaxTimeout time.Duration MaxRedirects int Client *http.Client SingleClient bool Usejar bool Errors []error DataAll interface{} Getter ClientGetter }
A HttpAgent is a object storing all request data for client.
func (*HttpAgent) AddCookie ¶
AddCookie adds a cookie to the request. The behavior is the same as AddCookie on Request from net/http
func (*HttpAgent) ClearAgent ¶
func (s *HttpAgent) ClearAgent()
Clear HttpAgent data for another new request.
func (*HttpAgent) End ¶
func (s *HttpAgent) End(callback ...func(response *http.Response, errs []error)) (*http.Response, []error)
End is the most important function that you need to call when ending the chain. The request won't proceed without calling it. End function returns Response which matchs the structure of Response type in Golang's http package (but without Body data). The body data itself returns as a string in a 2nd return value. Lastly but worht noticing, error array (NOTE: not just single error value) is returned as a 3rd value and nil otherwise.
For example:
resp, body, errs := gohttp.New().Get("http://www.google.com").End() if( errs != nil){ fmt.Println(errs) } fmt.Println(resp, body)
Moreover, End function also supports callback which you can put as a parameter. This extends the flexibility and makes gohttp fun and clean! You can use gohttp in whatever style you love!
For example:
func printBody(resp gohttp.Response, body string, errs []error){ fmt.Println(resp.Status) } gohttp.New().Get("http://www..google.com").End(printBody)
func (*HttpAgent) MaxRedirect ¶
func (*HttpAgent) Param ¶
As Go conventions accepts ; as a synonym for &. (https://github.com/golang/go/issues/2210) Thus, Query won't accept ; in a querystring if we provide something like fields=f1;f2;f3 This Param is then created as an alternative method to solve this.
func (*HttpAgent) Proxy ¶
Proxy function accepts a proxy url string to setup proxy url for any request. It provides a convenience way to setup proxy which have advantages over usual old ways. One example is you might try to set `http_proxy` environment. This means you are setting proxy up for all the requests. You will not be able to send different request with different proxy unless you change your `http_proxy` environment again. Another example is using Golang proxy setting. This is normal prefer way to do but too verbase compared to gohttp's Proxy:
gohttp.New().Proxy("http://myproxy:9999"). Post("http://www.google.com"). End()
To set no_proxy, just put empty string to Proxy func:
gohttp.New().Proxy(""). Post("http://www.google.com"). End()
func (*HttpAgent) Query ¶
Query function accepts either json string or strings which will form a query-string in url of GET method or body of POST method. For example, making "/search?query=bicycle&size=50x50&weight=20kg" using GET method:
gohttp.New(). Get("/search"). Query(`{ query: 'bicycle' }`). Query(`{ size: '50x50' }`). Query(`{ weight: '20kg' }`). End()
Or you can put multiple json values:
gohttp.New(). Get("/search"). Query(`{ query: 'bicycle', size: '50x50', weight: '20kg' }`). End()
Strings are also acceptable:
gohttp.New(). Get("/search"). Query("query=bicycle&size=50x50"). Query("weight=20kg"). End()
Or even Mixed! :)
gohttp.New(). Get("/search"). Query("query=bicycle"). Query(`{ size: '50x50', weight:'20kg' }`). End()
func (*HttpAgent) Send ¶
Send function accepts either json string or query strings which is usually used to assign data to POST or PUT method. Without specifying any type, if you give Send with json data, you are doing requesting in json format:
gohttp.New(). Post("/search"). Send(`{ query: 'sushi' }`). End()
While if you use at least one of querystring, gohttp understands and automatically set the Content-Type to `application/x-www-form-urlencoded`
gohttp.New(). Post("/search"). Send("query=tonkatsu"). End()
So, if you want to strictly send json format, you need to use Type func to set it as `json` (Please see more details in Type function). You can also do multiple chain of Send:
gohttp.New(). Post("/search"). Send("query=bicycle&size=50x50"). Send(`{ wheel: '4'}`). End()
From v0.2.0, Send function provide another convenience way to work with Struct type. You can mix and match it with json and query string:
type BrowserVersionSupport struct { Chrome string Firefox string } ver := BrowserVersionSupport{ Chrome: "37.0.2041.6", Firefox: "30.0" } gohttp.New(). Post("/update_version"). Send(ver). Send(`{"Safari":"5.1.10"}`). End()
func (*HttpAgent) SendFile ¶
SendFile function works only with type "multipart". The function accepts one mandatory and up to two optional arguments. The mandatory (first) argument is the file. The function accepts a path to a file as string:
gorequest.New(). Post("http://example.com"). Type("multipart"). SendFile("./example_file.ext"). End()
File can also be a []byte slice of a already file read by eg. ioutil.ReadFile:
b, _ := ioutil.ReadFile("./example_file.ext") gorequest.New(). Post("http://example.com"). Type("multipart"). SendFile(b). End()
Furthermore file can also be a os.File:
f, _ := os.Open("./example_file.ext") gorequest.New(). Post("http://example.com"). Type("multipart"). SendFile(f). End()
The first optional argument (second argument overall) is the filename, which will be automatically determined when file is a string (path) or a os.File. When file is a []byte slice, filename defaults to "filename". In all cases the automatically determined filename can be overwritten:
b, _ := ioutil.ReadFile("./example_file.ext") gorequest.New(). Post("http://example.com"). Type("multipart"). SendFile(b, "my_custom_filename"). End()
The second optional argument (third argument overall) is the fieldname in the multipart/form-data request. It defaults to fileNUMBER (eg. file1), where number is ascending and starts counting at 1. So if you send multiple files, the fieldnames will be file1, file2, ... unless it is overwritten. If fieldname is set to "file" it will be automatically set to fileNUMBER, where number is the greatest exsiting number+1.
b, _ := ioutil.ReadFile("./example_file.ext") gorequest.New(). Post("http://example.com"). Type("multipart"). SendFile(b, "", "my_custom_fieldname"). // filename left blank, will become "example_file.ext" End()
大文件建议传os.File进来
func (*HttpAgent) SendString ¶
SendString returns HttpAgent's itself for any next chain and takes content string as a parameter. Its duty is to transform String into s.Data (map[string]interface{}) which later changes into appropriate format such as json, form, text, etc. in the End func. Send implicitly uses SendString and you should use Send instead of this.
func (*HttpAgent) Set ¶
Set is used for setting header fields. Example. To set `Accept` as `application/json`
gohttp.New(). Post("/gamelist"). Set("Accept", "application/json"). End()
func (*HttpAgent) TLSClientConfig ¶
Set TLSClientConfig for underling Transport. One example is you can use it to disable security check (https):
gohttp.New().TLSClientConfig(&tls.Config{ InsecureSkipVerify: true}). Get("https://disable-security-check.com"). End()
func (*HttpAgent) Type ¶
Type is a convenience function to specify the data type to send. For example, to send data as `application/x-www-form-urlencoded` :
gohttp.New(). Post("/recipe"). Type("form"). Send(`{ name: "egg benedict", category: "brunch" }`). End()
This will POST the body "name=egg benedict&category=brunch" to url /recipe
gohttp supports
"text/html" uses "html" "application/json" uses "json" "application/xml" uses "xml" "application/x-www-form-urlencoded" uses "urlencoded", "form" or "form-data"
type IpRollClient ¶
type IpRollClient struct {
// contains filtered or unexported fields
}
func NewIpRollClient ¶
func NewIpRollClient(ip ...string) *IpRollClient
func (*IpRollClient) GetHttpClient ¶
func (*IpRollClient) ResetCookie ¶
func (s *IpRollClient) ResetCookie(uri *url.URL)
type MultipartStreamer ¶
type MultipartStreamer struct { ContentType string // contains filtered or unexported fields }
func NewMultiPartStreamer ¶
func NewMultiPartStreamer() (m *MultipartStreamer)
New initializes a new MultipartStreamer.
func (*MultipartStreamer) Boundary ¶
func (m *MultipartStreamer) Boundary() string
func (*MultipartStreamer) GetReader ¶
func (m *MultipartStreamer) GetReader() io.ReadCloser
GetReader gets an io.ReadCloser for passing to an http.Request.
func (*MultipartStreamer) Len ¶
func (m *MultipartStreamer) Len() int64
Len calculates the byte size of the multipart content.
func (*MultipartStreamer) SetupRequest ¶
func (m *MultipartStreamer) SetupRequest(req *http.Request)
SetupRequest sets up the http.Request body, and some crucial HTTP headers.
func (*MultipartStreamer) WriteFields ¶
func (m *MultipartStreamer) WriteFields(fields url.Values) error
WriteFields writes multiple form fields to the multipart.Writer.
func (*MultipartStreamer) WriteFile ¶
func (m *MultipartStreamer) WriteFile(key, filename string) error
WriteFile is a shortcut for adding a local file as an io.Reader.
func (*MultipartStreamer) WriteReader ¶
func (m *MultipartStreamer) WriteReader(f File) (err error)
WriteReader adds an io.Reader to get the content of a file. The reader is not accessed until the multipart.Reader is copied to some output writer. func (m *MultipartStreamer) WriteReader(key, filename string, size int64, reader io.Reader, ctype string) (err error) {