httpclient

package
v0.0.0-...-0569e1c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 18, 2021 License: Apache-2.0 Imports: 14 Imported by: 2

Documentation

Index

Constants

View Source
const (
	POST   = http.MethodPost
	GET    = http.MethodGet
	HEAD   = http.MethodHead
	PUT    = http.MethodPut
	DELETE = http.MethodDelete
)

HTTP methods we support

View Source
const (
	DEFAULT_USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36"
)

Variables

View Source
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",
	"csv":        "text/csv",
}

Functions

This section is empty.

Types

type Request

type Request *http.Request

type Response

type Response *http.Response

type SuperAgent

type SuperAgent struct {
	Url         string
	Method      string
	Header      map[string]string
	TargetType  string
	ForceType   string
	Data        map[string]interface{}
	RawBodyData string
	FormData    url.Values
	QueryData   url.Values
	Client      *http.Client
	Transport   *http.Transport
	Cookies     []*http.Cookie
	Errors      []error
	Log         bool // Log request and response
}

A SuperAgent is a object storing all request data for client.

func New

func New() *SuperAgent

Used to create a new SuperAgent object.

func (*SuperAgent) AddCookie

func (s *SuperAgent) AddCookie(c *http.Cookie) *SuperAgent

AddCookie adds a cookie to the request. The behavior is the same as AddCookie on Request from net/http

func (*SuperAgent) ClearSuperAgent

func (s *SuperAgent) ClearSuperAgent()

Clear SuperAgent data for another new request.

func (*SuperAgent) Debug

func (s *SuperAgent) Debug() *SuperAgent

func (*SuperAgent) Delete

func (s *SuperAgent) Delete(targetUrl string) *SuperAgent

func (*SuperAgent) End

func (s *SuperAgent) End(callback ...func(response Response, body string, errs []error)) (Response, string, []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 := gorequest.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 GoRequest fun and clean! You can use GoRequest in whatever style you love!

For example:

func printBody(resp gorequest.Response, body string, errs []error){
  fmt.Println(resp.Status)
}
gorequest.New().Get("http://www..google.com").End(printBody)

func (*SuperAgent) Get

func (s *SuperAgent) Get(targetUrl string) *SuperAgent

func (*SuperAgent) Head

func (s *SuperAgent) Head(targetUrl string) *SuperAgent

func (*SuperAgent) Post

func (s *SuperAgent) Post(targetUrl string) *SuperAgent

func (*SuperAgent) Proxy

func (s *SuperAgent) Proxy(proxyUrl string) *SuperAgent

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 GoRequest's Proxy:

gorequest.New().Proxy("http://myproxy:9999").
  Post("http://www.google.com").
  End()

To set no_proxy, just put empty string to Proxy func:

gorequest.New().Proxy("").
  Post("http://www.google.com").
  End()

func (*SuperAgent) Put

func (s *SuperAgent) Put(targetUrl string) *SuperAgent

func (*SuperAgent) Query

func (s *SuperAgent) Query(content string) *SuperAgent

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:

gorequest.New().
  Get("/search").
  Query(`{ query: 'bicycle' }`).
  Query(`{ size: '50x50' }`).
  Query(`{ weight: '20kg' }`).
  End()

Or you can put multiple json values:

gorequest.New().
  Get("/search").
  Query(`{ query: 'bicycle', size: '50x50', weight: '20kg' }`).
  End()

Strings are also acceptable:

gorequest.New().
  Get("/search").
  Query("query=bicycle&size=50x50").
  Query("weight=20kg").
  End()

Or even Mixed! :)

gorequest.New().
  Get("/search").
  Query("query=bicycle").
  Query(`{ size: '50x50', weight:'20kg' }`).
  End()

func (*SuperAgent) RedirectPolicy

func (s *SuperAgent) RedirectPolicy(policy func(req Request, via []Request) error) *SuperAgent

func (*SuperAgent) Send

func (s *SuperAgent) Send(content interface{}) *SuperAgent

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:

gorequest.New().
  Post("/search").
  Send(`{ query: 'sushi' }`).
  End()

While if you use at least one of querystring, GoRequest understands and automatically set the Content-Type to `application/x-www-form-urlencoded`

gorequest.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:

gorequest.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" }
gorequest.New().
  Post("/update_version").
  Send(ver).
  Send(`{"Safari":"5.1.10"}`).
  End()

func (*SuperAgent) SendRawBodyData

func (s *SuperAgent) SendRawBodyData(content string) *SuperAgent

SendRawBodyData returns SuperAgent's itself for any next chain and takes content string as a parameter. Its duty is to put content to s.RawBodyData which later as the raw body content of the request

func (*SuperAgent) SendString

func (s *SuperAgent) SendString(content string) *SuperAgent

SendString returns SuperAgent'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 (*SuperAgent) Set

func (s *SuperAgent) Set(param string, value string) *SuperAgent

Set is used for setting header fields. Example. To set `Accept` as `application/json`

gorequest.New().
  Post("/gamelist").
  Set("Content-Type", "application/json").
  Set("Accept", "application/json").
  End()

func (*SuperAgent) TLSClientConfig

func (s *SuperAgent) TLSClientConfig(config *tls.Config) *SuperAgent

Set TLSClientConfig for underling Transport. One example is you can use it to disable security check (https):

gorequest.New().TLSClientConfig(&tls.Config{ InsecureSkipVerify: true}).
    Get("https://disable-security-check.com").
    End()

func (*SuperAgent) Timeout

func (s *SuperAgent) Timeout(timeout time.Duration) *SuperAgent

func (*SuperAgent) Type

func (s *SuperAgent) Type(typeStr string) *SuperAgent

Type is a convenience function to specify the data type to send. For example, to send data as `application/x-www-form-urlencoded` :

gorequest.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

GoRequest supports

"text/html" uses "html"
"application/json" uses "json"
"application/xml" uses "xml"
"application/x-www-form-urlencoded" uses "urlencoded", "form" or "form-data"
"text/csv" uses csv

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL