Documentation ¶
Overview ¶
Example (Factory) ¶
package main import ( "fmt" "time" "github.com/chyroc/requests" ) func main() { // I hope to set fixed parameters every time I initiate a request // Then, every request created by this factory will not log opt := requests.Options( requests.WithLogger(requests.DiscardLogger()), requests.WithTimeout(time.Second*10), requests.WithQuery("query", "value"), requests.WithHeader("Auth", "hey"), ) // Send sample request r := requests.Get("https://httpbin.org/get", opt...) r = requests.Post("https://httpbin.org/get"). WithLogger(requests.DiscardLogger()). WithTimeout(time.Second * 10) fmt.Println(r.Text().Unpack()) }
Output:
Example (NewSession) ¶
package main import ( "fmt" "time" "github.com/chyroc/requests" ) func main() { session := requests.NewSession("/tmp/requests-session.txt") r := session.Get("https://jsonplaceholder.typicode.com/todos/1"). WithTimeout(time.Second * 10) fmt.Println(r.Text().Unpack()) }
Output:
Example (Unmarshal) ¶
package main import ( "fmt" "github.com/chyroc/anyhow" "github.com/chyroc/requests" ) func main() { type Data struct{} f := func() (Data, error) { type Response struct { Code int32 Data Data } return anyhow.AndThen11(requests.JSON[Response](requests.Post("https://httpbin.org/post")), func(t1 *Response) anyhow.Result1[Data] { if t1.Code != 0 { return anyhow.Err1[Data](fmt.Errorf("fail: %d", t1.Code)) } return anyhow.Ok1(t1.Data) }).Unpack() } f() }
Output:
Index ¶
- func JSON[T any](r *Request) Result1[*T]
- type Logger
- type Option
- type Request
- func Delete(url string, options ...Option) *Request
- func Get(url string, options ...Option) *Request
- func New(method, url string, options ...Option) *Request
- func Patch(url string, options ...Option) *Request
- func Post(url string, options ...Option) *Request
- func Put(url string, options ...Option) *Request
- func (r *Request) Bytes() Result1[[]byte]
- func (r *Request) Context() context.Context
- func (r *Request) CookiesByKey(key string) Result1[[]string]
- func (r *Request) FullURL() string
- func (r *Request) Header() Result1[http.Header]
- func (r *Request) HeaderByKey(key string) Result1[string]
- func (r *Request) HeadersByKey(key string) Result1[[]string]
- func (r *Request) Map() Result1[map[string]any]
- func (r *Request) Method() string
- func (r *Request) ReqHeader() http.Header
- func (r *Request) Response() Result1[*http.Response]
- func (r *Request) SetError(err error) *Request
- func (r *Request) Status() Result1[int]
- func (r *Request) Text() Result1[string]
- func (r *Request) Timeout() time.Duration
- func (r *Request) URL() string
- func (r *Request) WithBody(body any) *Request
- func (r *Request) WithContext(ctx context.Context) *Request
- func (r *Request) WithFile(filename string, file io.Reader, fileKey string, params map[string]string) *Request
- func (r *Request) WithForm(body map[string]string) *Request
- func (r *Request) WithFormURLEncoded(body map[string]string) *Request
- func (r *Request) WithHeader(k, v string) *Request
- func (r *Request) WithHeaders(kv map[string]string) *Request
- func (r *Request) WithIgnoreSSL(ignore bool) *Request
- func (r *Request) WithJSON(body any) *Request
- func (r *Request) WithLogger(logger Logger) *Request
- func (r *Request) WithQueries(queries any) *Request
- func (r *Request) WithQuery(k, v string) *Request
- func (r *Request) WithRedirect(redirect bool) *Request
- func (r *Request) WithTimeout(timeout time.Duration) *Request
- func (r *Request) WithURLCookie(uri string) *Request
- func (r *Request) WithWrapResponse(f func(resp *http.Response) (*http.Response, error)) *Request
- type Session
- func (r *Session) CookieFile() string
- func (r *Session) Delete(url string, options ...Option) *Request
- func (r *Session) Get(url string, options ...Option) *Request
- func (r *Session) Jar() http.CookieJar
- func (r *Session) New(method, url string, options ...Option) *Request
- func (r *Session) Patch(url string, options ...Option) *Request
- func (r *Session) Post(url string, options ...Option) *Request
- func (r *Session) Put(url string, options ...Option) *Request
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Logger ¶
type Logger interface { Info(ctx context.Context, format string, v ...any) Error(ctx context.Context, format string, v ...any) }
func DiscardLogger ¶
func DiscardLogger() Logger
func StdoutLogger ¶
func StdoutLogger() Logger
type Option ¶
type Option func(req *Request)
func WithHeader ¶
func WithHeaders ¶ added in v0.1.1
func WithLogger ¶
func WithQueries ¶ added in v0.1.1
func WithTimeout ¶
type Request ¶
type Request struct {
// contains filtered or unexported fields
}
func (*Request) CookiesByKey ¶
CookiesByKey get specific http cookie response with key
func (*Request) HeaderByKey ¶
HeaderByKey get specific http header response with key
func (*Request) HeadersByKey ¶
HeadersByKey get specific http header response with key
func (*Request) WithBody ¶
WithBody set request body, support: io.Reader, []byte, string, any(as json format)
func (*Request) WithContext ¶
WithContext setup request context.Context
func (*Request) WithFile ¶
func (r *Request) WithFile(filename string, file io.Reader, fileKey string, params map[string]string) *Request
WithFile set file to body and set some multi-form k-v map
func (*Request) WithFormURLEncoded ¶
WithFormURLEncoded set body and set Content-Type to application/x-www-form-urlencoded
func (*Request) WithHeader ¶
WithHeader set one header k-v map
func (*Request) WithHeaders ¶
WithHeaders set multi header k-v map
func (*Request) WithIgnoreSSL ¶
WithIgnoreSSL ignore ssl verify
func (*Request) WithJSON ¶
WithJSON set body same as WithBody, and set Content-Type to application/json
func (*Request) WithLogger ¶
WithLogger set logger
func (*Request) WithQueries ¶ added in v0.1.1
WithQueries set multi query k-v
func (*Request) WithRedirect ¶
WithRedirect set allow or not-allow redirect with Location header
func (*Request) WithTimeout ¶
WithTimeout setup request timeout
func (*Request) WithURLCookie ¶
WithURLCookie set cookie of uri
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
func NewSession ¶
same cookie-file has same session instance