Documentation ¶
Overview ¶
Package goozzle - PHP Guzzle flavoured HTTP client for golang
Index ¶
- Constants
- Variables
- type DebugHandler
- type Request
- func (r *Request) Body(body []byte) (*Response, error)
- func (r *Request) Cookie(cookie *http.Cookie) *Request
- func (r *Request) Debug(h DebugHandler) *Request
- func (r *Request) Do() (*Response, error)
- func (r *Request) Form(v url.Values) (*Response, error)
- func (r *Request) Header(key, value string) *Request
- func (r *Request) Headers() map[string]string
- func (r *Request) JSON(v interface{}) (*Response, error)
- func (r *Request) Method() string
- func (r *Request) Referer(referer string) *Request
- func (r *Request) String() string
- func (r *Request) URL() *url.URL
- func (r *Request) UserAgent(userAgent string) *Request
- type Response
- func (r *Response) Body() []byte
- func (r *Response) Cookie(key string) string
- func (r *Response) Cookies() []*http.Cookie
- func (r *Response) Header(key string) string
- func (r *Response) Headers() map[string]string
- func (r *Response) JSON(v interface{}) error
- func (r *Response) Request() *Request
- func (r *Response) Status() int
- func (r *Response) String() string
Examples ¶
Constants ¶
View Source
const ( // Version goozzle version Version = 1 // UserAgentChrome if you want to pretend to be Сhrome UserAgentChrome = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36" // UserAgentSafari if you want to pretend to be Safari UserAgentSafari = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/604.4.7 (KHTML, like Gecko) Version/11.0.2 Safari/604.4.7" // UserAgentFirefox if you want to pretend to be Firefox UserAgentFirefox = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:63.0) Gecko/20100101 Firefox/63.0" // UserAgentInternetExplorer if you want to pretend to be IE UserAgentInternetExplorer = "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; AS; rv:11.0) like Gecko" // UserAgentEdge if you want to pretend to be Edge UserAgentEdge = "" /* 153-byte string literal not displayed */ )
Variables ¶
View Source
var UserAgentDefault = fmt.Sprintf("Goozzle/%d", Version)
UserAgentDefault default goozzle user agent
Functions ¶
This section is empty.
Types ¶
type DebugHandler ¶
type DebugHandler func(*Response)
DebugHandler handler function to debug requests
type Request ¶
type Request struct {
// contains filtered or unexported fields
}
Request HTTP-request struct
func (*Request) Debug ¶
func (r *Request) Debug(h DebugHandler) *Request
Debug sets request debug handler func
func (*Request) Do ¶
Do returns response
Example ¶
package main import ( "fmt" "github.com/alexeyco/goozzle" "log" "net/url" ) func main() { u, _ := url.Parse("https://jsonplaceholder.typicode.com/posts/1") _, err := goozzle.Get(u).Debug(func(res *goozzle.Response) { req := res.Request() fmt.Println("Request") fmt.Println("=======") fmt.Println("") fmt.Println("URL:", req.URL().String()) fmt.Println("") fmt.Println("Headers:") for key, val := range req.Headers() { fmt.Println(fmt.Sprintf("%s: %s", key, val)) } fmt.Println("") fmt.Println("Response") fmt.Println("=======") fmt.Println("") fmt.Println("Status:", res.Status()) fmt.Println("") fmt.Println("Headers:") for key, val := range res.Headers() { fmt.Println(fmt.Sprintf("%s: %s", key, val)) } fmt.Println("") fmt.Println("Body:") fmt.Println(res.String()) fmt.Println("") }).Do() if err != nil { log.Fatal(err) } }
Output:
func (*Request) JSON ¶
JSON sets request JSON and returns response
Example ¶
package main import ( "fmt" "github.com/alexeyco/goozzle" "log" "net/url" ) func main() { type Post struct { ID int `json:"id"` UserID int `json:"userId"` Title string `json:"title"` Body string `json:"body"` } u, _ := url.Parse("https://jsonplaceholder.typicode.com/posts") post := &Post{ ID: 999, UserID: 888, Title: "Some title", Body: "Many hands make light work", } _, err := goozzle.Post(u).Debug(func(res *goozzle.Response) { req := res.Request() fmt.Println("Request") fmt.Println("=======") fmt.Println("") fmt.Println("URL:", req.URL().String()) fmt.Println("Method:", req.Method()) fmt.Println("") fmt.Println("Headers:") for key, val := range req.Headers() { fmt.Println(fmt.Sprintf("%s: %s", key, val)) } fmt.Println("") fmt.Println("Body:") fmt.Println(req.String()) fmt.Println("") fmt.Println("Response") fmt.Println("=======") fmt.Println("") fmt.Println("Status:", res.Status()) fmt.Println("") fmt.Println("Headers:") for key, val := range res.Headers() { fmt.Println(fmt.Sprintf("%s: %s", key, val)) } fmt.Println("") fmt.Println("Body:") fmt.Println(res.String()) fmt.Println("") }).JSON(&post) if err != nil { log.Fatal(err) } }
Output:
type Response ¶
type Response struct {
// contains filtered or unexported fields
}
Response HTTP-response struct
func (*Response) JSON ¶
JSON unmarshal JSON response to struct
Example ¶
package main import ( "github.com/alexeyco/goozzle" "log" "net/url" ) func main() { type Post struct { ID int `json:"id"` UserID int `json:"userId"` Title string `json:"title"` Body string `json:"body"` } u, _ := url.Parse("https://jsonplaceholder.typicode.com/posts/1") res, err := goozzle.Get(u).Do() if err != nil { log.Fatal(err) } var post Post err = res.JSON(&post) if err != nil { log.Fatal(err) } log.Println(post) }
Output:
Click to show internal directories.
Click to hide internal directories.