Documentation
¶
Overview ¶
Example ¶
resp, err := Post("https://httpbin.org/post", nil, url.Values{"hello": []string{"world"}}) if err != nil { log.Fatal(err) } var data struct { Form struct{ Hello string } } if err := resp.JSON(&data); err != nil { log.Fatal(err) } fmt.Println(data.Form.Hello) // world
Output:
Index ¶
- func SetAgent(agent string)
- func SetClient(c *http.Client)
- func SetNoProxy()
- func SetProxy(proxy string) error
- func SetProxyFromEnvironment()
- func SetTimeout(d time.Duration)
- type File
- type H
- type Response
- func Do(req *http.Request) (*Response, error)
- func Get(url string, headers H) (*Response, error)
- func GetWithClient(ctx context.Context, url string, headers H, client *http.Client) (*Response, error)
- func GetWithContext(ctx context.Context, url string, headers H) (*Response, error)
- func Head(url string, headers H) (*Response, error)
- func HeadWithClient(ctx context.Context, url string, headers H, client *http.Client) (*Response, error)
- func HeadWithContext(ctx context.Context, url string, headers H) (*Response, error)
- func Post(url string, headers H, data any) (*Response, error)
- func PostWithClient(ctx context.Context, url string, headers H, data any, client *http.Client) (*Response, error)
- func PostWithContext(ctx context.Context, url string, headers H, data any) (*Response, error)
- func Upload(url string, headers H, params map[string]string, files ...*File) (*Response, error)
- func UploadWithClient(ctx context.Context, url string, headers H, params map[string]string, ...) (*Response, error)
- func UploadWithContext(ctx context.Context, url string, headers H, params map[string]string, ...) (*Response, error)
- func (r *Response) Bytes() []byte
- func (r *Response) Close() error
- func (r *Response) Cookies() []*http.Cookie
- func (r *Response) JSON(data any) error
- func (r *Response) Raw() *http.Response
- func (r *Response) Read(p []byte) (int, error)
- func (r *Response) Request() *http.Request
- func (r *Response) Save(file string) (int, error)
- func (r *Response) String() string
- type Session
- func (s *Session) Cookies(u *url.URL) []*http.Cookie
- func (s *Session) Do(req *http.Request) (*Response, error)
- func (s *Session) Get(url string, headers H) (*Response, error)
- func (s *Session) GetWithContext(ctx context.Context, url string, headers H) (*Response, error)
- func (s *Session) Head(url string, headers H) (*Response, error)
- func (s *Session) HeadWithContext(ctx context.Context, url string, headers H) (*Response, error)
- func (s *Session) KeepAlive(interval *time.Duration, fn func(*Session) error) (err error)
- func (s *Session) Post(url string, headers H, data any) (*Response, error)
- func (s *Session) PostWithContext(ctx context.Context, url string, headers H, data any) (*Response, error)
- func (s *Session) SetClient(c *http.Client)
- func (s *Session) SetCookie(u *url.URL, name, value string)
- func (s *Session) SetCookies(u *url.URL, cookies []*http.Cookie)
- func (s *Session) SetNoProxy()
- func (s *Session) SetProxy(proxy string) error
- func (s *Session) SetProxyFromEnvironment()
- func (s *Session) SetTimeout(d time.Duration)
- func (s *Session) Upload(url string, headers H, params map[string]string, files ...*File) (*Response, error)
- func (s *Session) UploadWithContext(ctx context.Context, url string, headers H, params map[string]string, ...) (*Response, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SetProxyFromEnvironment ¶
func SetProxyFromEnvironment()
SetProxyFromEnvironment sets default client use environment proxy.
func SetTimeout ¶ added in v1.0.6
SetTimeout sets default timeout. Zero means no timeout.
Types ¶
type File ¶
type File struct { io.ReadCloser Fieldname string Filename string }
File contains the file part of a multipart message.
type Response ¶
type Response struct { // StatusCode represents the response status code. StatusCode int // Header maps header keys to values. Header http.Header // ContentLength records the length of the associated content. ContentLength int64 // contains filtered or unexported fields }
Response represents the response from an HTTP request.
func GetWithClient ¶
func GetWithClient(ctx context.Context, url string, headers H, client *http.Client) (*Response, error)
GetWithClient issues a GET to the specified URL with context, headers and client.
func GetWithContext ¶ added in v1.0.6
GetWithContext issues a GET to the specified URL with context and headers.
func HeadWithClient ¶
func HeadWithClient(ctx context.Context, url string, headers H, client *http.Client) (*Response, error)
HeadWithClient issues a HEAD to the specified URL with context, headers and client.
func HeadWithContext ¶ added in v1.0.6
HeadWithContext issues a HEAD to the specified URL with context and headers.
func Post ¶
Post issues a POST to the specified URL with headers. Post data should be one of nil, io.Reader, url.Values, string map or struct.
func PostWithClient ¶
func PostWithClient(ctx context.Context, url string, headers H, data any, client *http.Client) (*Response, error)
PostWithClient issues a POST to the specified URL with context, headers and client.
func PostWithContext ¶ added in v1.0.6
PostWithContext issues a POST to the specified URL with context and headers. Post data should be one of nil, io.Reader, url.Values, string map or struct.
func Upload ¶
Upload issues a POST to the specified URL with a multipart document.
Example ¶
resp, err := Upload("https://httpbin.org/post", nil, nil, F("readme", "README.md")) if err != nil { log.Fatal(err) } var data struct { Files struct{ Readme string } Headers struct { ContentType string `json:"Content-Type"` } } if err := resp.JSON(&data); err != nil { log.Fatal(err) } fmt.Println(strings.Split(data.Headers.ContentType, ";")[0]) // multipart/form-data
Output:
func UploadWithClient ¶
func UploadWithClient(ctx context.Context, url string, headers H, params map[string]string, files []*File, client *http.Client) (*Response, error)
UploadWithClient issues a POST to the specified URL with context, a multipart document and client.
func UploadWithContext ¶ added in v1.0.6
func UploadWithContext(ctx context.Context, url string, headers H, params map[string]string, files ...*File) (*Response, error)
UploadWithContext issues a POST to the specified URL with context and a multipart document.
func (*Response) Cookies ¶ added in v1.0.7
Cookies parses and returns the cookies set in the Set-Cookie headers.
func (*Response) JSON ¶
JSON parses the response body as JSON-encoded data and stores the result in the value pointed to by data.
func (*Response) Request ¶ added in v1.0.7
Request is the request that was sent to obtain this Response.
type Session ¶
Session provides cookie persistence and configuration.
Example ¶
s := NewSession() s.Header.Set("hello", "world") s.Get("https://httpbin.org/cookies/set/name/value", nil) resp, err := s.Get("https://httpbin.org/get", nil) if err != nil { log.Fatal(err) } var data struct { Headers struct{ Hello, Cookie string } } if err := resp.JSON(&data); err != nil { log.Fatal(err) } fmt.Println(data.Headers.Hello, data.Headers.Cookie) // world name=value
Output:
func NewSession ¶
func NewSession() *Session
NewSession creates and initializes a new Session using initial contents.
func (*Session) GetWithContext ¶ added in v1.0.6
GetWithContext issues a session GET to the specified URL with context and additional headers.
func (*Session) HeadWithContext ¶ added in v1.0.6
HeadWithContext issues a session HEAD to the specified URL with context and additional headers.
func (*Session) KeepAlive ¶ added in v1.0.1
KeepAlive repeatedly calls fn with a fixed interval delay between each call.
func (*Session) PostWithContext ¶ added in v1.0.6
func (s *Session) PostWithContext(ctx context.Context, url string, headers H, data any) (*Response, error)
PostWithContext issues a session POST to the specified URL with context and additional headers.
func (*Session) SetCookie ¶
SetCookie handles the receipt of the cookie in a reply for the given URL.
func (*Session) SetCookies ¶
SetCookies handles the receipt of the cookies in a reply for the given URL.
func (*Session) SetNoProxy ¶
func (s *Session) SetNoProxy()
SetNoProxy sets Session client use no proxy.
func (*Session) SetProxyFromEnvironment ¶
func (s *Session) SetProxyFromEnvironment()
SetProxyFromEnvironment sets Session client use environment proxy.
func (*Session) SetTimeout ¶
SetTimeout sets Session client timeout. Zero means no timeout.
func (*Session) Upload ¶
func (s *Session) Upload(url string, headers H, params map[string]string, files ...*File) (*Response, error)
Upload issues a session POST to the specified URL with a multipart document and additional headers.
func (*Session) UploadWithContext ¶ added in v1.0.6
func (s *Session) UploadWithContext(ctx context.Context, url string, headers H, params map[string]string, files ...*File) (*Response, error)
UploadWithContext issues a session POST to the specified URL with context, a multipart document and additional headers.