gorequests

package module
Version: v0.27.0 Latest Latest
Warning

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

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

README

gorequests

codecov go report card test status Apache-2.0 license Go.Dev reference Go project version

Simple and easy-to-use go http client, supports cookies, streaming calls, custom logs and other functions

Install

go get github.com/chyroc/gorequests

Usage

Simple Send Request
func main() {
	text, err := gorequests.New(http.MethodGet, "https://jsonplaceholder.typicode.com/todos/1").Text()
	if err != nil {
		panic(err)
	}
	fmt.Println("text", text)
}
func main() {
	session := gorequests.NewSession("/tmp/gorequests-session.txt")
	text, err := session.New(http.MethodGet, "https://jsonplaceholder.typicode.com/todos/1").Text()
	if err != nil {
		panic(err)
	}
	fmt.Println("text", text)
}
Request Factory
func main() {
    fac := gorequests.NewFactory(
        gorequests.WithLogger(gorequests.NewDiscardLogger()),
    )
	text, err := fac.New(http.MethodGet, "https://jsonplaceholder.typicode.com/todos/1").Text()
	if err != nil {
		panic(err)
	}
	fmt.Println("text", text)
}

Documentation

Overview

Package gorequests send http request with human style.

for send GET request:

gorequests.New(http.MethodGet, "https://httpbin.org/get")

for send POST request:

gorequests.New(http.MethodPost, "https://httpbin.org/post).WithBody("text body")

for send http upload request

gorequests.New(http.MethodPost, "https://httpbin.org/post).WithFile("1.txt", strings.NewReader("hi"), "file", nil)

for send json request

gorequests.New(http.MethodPost, "https://httpbin.org/post).WithJSON(map[string]string{"key": "val"})

request with timeout

gorequests.New(http.MethodGet, "https://httpbin.org/get).WithTimeout(time.Second)

request with context

gorequests.New(http.MethodGet, "https://httpbin.org/get).WithContext(context.TODO())

request with no redirect

gorequests.New(http.MethodGet, "https://httpbin.org/status/302).WithRedirect(false)
Example (Factory)
package main

import (
	"fmt"
	"net/http"
	"time"

	"github.com/chyroc/gorequests"
)

func main() {
	// I hope to set fixed parameters every time I initiate a request

	// Then, every request created by this factory will not log
	fac := gorequests.NewFactory(
		gorequests.WithLogger(gorequests.NewDiscardLogger()),
		gorequests.WithTimeout(time.Second*10),
	)

	// Send sample request
	text, err := fac.New(http.MethodGet, "https://httpbin.org/get").Text()
	if err != nil {
		panic(err)
	}
	fmt.Println("text", text)
}
Output:

Example (New)
package main

import (
	"fmt"
	"net/http"
	"time"

	"github.com/chyroc/gorequests"
)

func main() {
	text, err := gorequests.New(http.MethodGet, "https://httpbin.org/get").WithTimeout(time.Second * 10).Text()
	if err != nil {
		panic(err)
	}
	fmt.Println("text", text)
}
Output:

Example (NewSession)
package main

import (
	"fmt"
	"net/http"
	"time"

	"github.com/chyroc/gorequests"
)

func main() {
	session := gorequests.NewSession("/tmp/gorequests-session.txt")
	text, err := session.New(http.MethodGet, "https://jsonplaceholder.typicode.com/todos/1").WithTimeout(time.Second * 10).Text()
	if err != nil {
		panic(err)
	}
	fmt.Println("text", text)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Factory added in v0.27.0

type Factory struct {
	// contains filtered or unexported fields
}

func NewFactory added in v0.27.0

func NewFactory(options ...RequestOption) *Factory

func (*Factory) New added in v0.27.0

func (r *Factory) New(method, url string) *Request

type Logger added in v0.10.0

type Logger interface {
	Info(ctx context.Context, format string, v ...interface{})
	Error(ctx context.Context, format string, v ...interface{})
}

func NewDiscardLogger added in v0.24.0

func NewDiscardLogger() Logger

func NewStdoutLogger added in v0.24.0

func NewStdoutLogger() Logger

type Request

type Request struct {
	// contains filtered or unexported fields
}

func New

func New(method, url string) *Request

func (*Request) Bytes

func (r *Request) Bytes() ([]byte, error)

func (*Request) Context added in v0.10.0

func (r *Request) Context() context.Context

Context request context.Context

func (*Request) Map

func (r *Request) Map() (map[string]interface{}, error)

func (*Request) Method

func (r *Request) Method() string

Method request method

func (*Request) MustBytes added in v0.19.0

func (r *Request) MustBytes() []byte

func (*Request) MustMap added in v0.19.0

func (r *Request) MustMap() map[string]interface{}

func (*Request) MustResponse added in v0.19.0

func (r *Request) MustResponse() *http.Response

func (*Request) MustResponseHeaderByKey added in v0.20.0

func (r *Request) MustResponseHeaderByKey(key string) string

func (*Request) MustResponseHeaders added in v0.19.0

func (r *Request) MustResponseHeaders() http.Header

func (*Request) MustResponseHeadersByKey added in v0.20.0

func (r *Request) MustResponseHeadersByKey(key string) []string

func (*Request) MustResponseStatus added in v0.19.0

func (r *Request) MustResponseStatus() int

func (*Request) MustText added in v0.19.0

func (r *Request) MustText() string

func (*Request) MustUnmarshal added in v0.19.0

func (r *Request) MustUnmarshal(val interface{})

func (*Request) RequestFullURL added in v0.19.0

func (r *Request) RequestFullURL() string

RequestFullURL request full url, contain query param

func (*Request) RequestHeader added in v0.19.0

func (r *Request) RequestHeader() http.Header

RequestHeader request header

func (*Request) Response added in v0.5.0

func (r *Request) Response() (*http.Response, error)

func (*Request) ResponseHeaderByKey added in v0.20.0

func (r *Request) ResponseHeaderByKey(key string) (string, error)

func (*Request) ResponseHeaders added in v0.19.0

func (r *Request) ResponseHeaders() (http.Header, error)

func (*Request) ResponseHeadersByKey added in v0.20.0

func (r *Request) ResponseHeadersByKey(key string) ([]string, error)

func (*Request) ResponseStatus added in v0.5.0

func (r *Request) ResponseStatus() (int, error)

func (*Request) SetError added in v0.6.0

func (r *Request) SetError(err error) *Request

func (*Request) Text

func (r *Request) Text() (string, error)

func (*Request) Timeout

func (r *Request) Timeout() time.Duration

Timeout request timeout

func (*Request) URL

func (r *Request) URL() string

URL request url

func (*Request) Unmarshal

func (r *Request) Unmarshal(val interface{}) error

func (*Request) WithBody

func (r *Request) WithBody(body interface{}) *Request

WithBody set request body, support: io.Reader, []byte, string, interface{}(as json format)

func (*Request) WithContext added in v0.19.0

func (r *Request) WithContext(ctx context.Context) *Request

WithContext setup request context.Context

func (*Request) WithFile added in v0.18.0

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) WithForm added in v0.10.0

func (r *Request) WithForm(body map[string]string) *Request

WithForm set body and set Content-Type to multiform

func (*Request) WithFormURLEncoded added in v0.25.0

func (r *Request) WithFormURLEncoded(body map[string]string) *Request

WithFormURLEncoded set body and set Content-Type to application/x-www-form-urlencoded

func (*Request) WithHeader

func (r *Request) WithHeader(k, v string) *Request

WithHeader set one header k-v map

func (*Request) WithHeaders

func (r *Request) WithHeaders(kv map[string]string) *Request

WithHeaders set multi header k-v map

func (*Request) WithIgnoreSSL added in v0.17.0

func (r *Request) WithIgnoreSSL(ignore bool) *Request

WithIgnoreSSL ignore ssl verify

func (*Request) WithJSON added in v0.16.0

func (r *Request) WithJSON(body interface{}) *Request

WithJSON set body same as WithBody, and set Content-Type to application/json

func (*Request) WithLogger added in v0.21.0

func (r *Request) WithLogger(logger Logger) *Request

WithLogger set logger

func (*Request) WithQuery added in v0.9.0

func (r *Request) WithQuery(k, v string) *Request

WithQuery set one query k-v map

func (*Request) WithQueryStruct added in v0.15.0

func (r *Request) WithQueryStruct(v interface{}) *Request

WithQueryStruct set multi query k-v map

func (*Request) WithQuerys added in v0.9.0

func (r *Request) WithQuerys(kv map[string]string) *Request

WithQuerys set multi query k-v map

func (*Request) WithRedirect added in v0.8.0

func (r *Request) WithRedirect(b bool) *Request

WithRedirect set allow or not-allow redirect with Location header

func (*Request) WithTimeout added in v0.19.0

func (r *Request) WithTimeout(timeout time.Duration) *Request

WithTimeout setup request timeout

func (*Request) WithURLCookie added in v0.16.0

func (r *Request) WithURLCookie(uri string) *Request

WithURLCookie set cookie of uri

type RequestOption added in v0.27.0

type RequestOption func(req *Request) error

func WithLogger added in v0.27.0

func WithLogger(logger Logger) RequestOption

func WithTimeout added in v0.27.0

func WithTimeout(timeout time.Duration) RequestOption

type Session

type Session struct {
	// contains filtered or unexported fields
}

func NewSession

func NewSession(cookiefile string) *Session

same cookie-file has same session instance

func (*Session) CookieFile added in v0.12.0

func (r *Session) CookieFile() string

func (*Session) Jar added in v0.14.0

func (r *Session) Jar() http.CookieJar

func (*Session) New

func (r *Session) New(method, url string) *Request

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL