curl

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2018 License: Apache-2.0 Imports: 20 Imported by: 0

README

go-curl

Go Report Card GoDoc

A Go HTTP client library for creating and sending API requests

Examples

import "github.com/subchen/go-curl"
Basic request
req := curl.NewRequest()
req.Method = "GET"
req.URL = "http://example.com/api/users"
resp, err := req.Do()
if err != nil {
	log.Fatalln("Unable to make request: ", err)
}
fmt.Println(resp.Text())
Chained request
user := newUser()
req := curl.NewRequest()
resp, err := req.SetBasicAuth("admin", "passwd").SetJSON(user).Post("http://example.com/api/users")
if err != nil {
	log.Fatalln("Unable to make request: ", err)
}
fmt.Println(resp.Text())

Documentation

Index

Constants

View Source
const (
	DefaultPayloadContentType = "application/octoc-streams"
	DefaultJsonContentType    = "application/json; charset=utf-8"
	DefaultFormContentType    = "application/x-www-form-urlencoded; charset=utf-8"
)

Variables

View Source
var (
	DefaultRequestTimeout      = 30 * time.Second
	DefaultDailTimeout         = 30 * time.Second
	DefaultTLSHandshakeTimeout = 30 * time.Second
	DefaultInsecureSkipVerify  = false
)
View Source
var DefaultHeaders = map[string]string{
	"Connection":      "keep-alive",
	"Accept-Encoding": "gzip, deflate",
	"Accept":          "*/*",
	"User-Agent":      DefaultUserAgent,
}
View Source
var DefaultRedirectLimit = 10

DefaultRedirectLimit define max redirect counts

View Source
var DefaultUserAgent = "subchen/go-curl"
View Source
var ErrMaxRedirect = errors.New("Exceeded max redirects")

ErrMaxRedirect when redirect times great than DefaultRedirectLimit will return this error

Functions

This section is empty.

Types

type BasicAuth

type BasicAuth struct {
	Username string
	Password string
}

func (*BasicAuth) RequestHeaderValue

func (a *BasicAuth) RequestHeaderValue() string

type Request

type Request struct {
	Client    *http.Client
	Transport *http.Transport

	Method           string
	URL              string
	QueryString      interface{} // url.Values, map[string]string, map[string][]string
	Headers          map[string]string
	Cookies          map[string]string
	Body             interface{} // io.Reader, string
	JSON             interface{} // any
	Form             interface{} // url.Values, map[string]string, map[string][]string
	Files            []UploadFile
	Auth             interface{} // authorization(BasicAuth, TokenAuth, ...), string
	ProxyURL         string      // http(s)://..., sock5://...
	RedirectDisabled bool
}

func NewRequest

func NewRequest() *Request

func (*Request) AddCookie

func (r *Request) AddCookie(name, value string) *Request

func (*Request) AddFile

func (r *Request) AddFile(field, filename string) *Request

func (*Request) AddHeader

func (r *Request) AddHeader(name, value string) *Request

func (*Request) Delete

func (r *Request) Delete(url string) (*Response, error)

func (*Request) Do

func (r *Request) Do() (*Response, error)

func (*Request) Get

func (r *Request) Get(url string) (*Response, error)

func (*Request) Head

func (r *Request) Head(url string) (*Response, error)

func (*Request) Options

func (r *Request) Options(url string) (*Response, error)

func (*Request) Patch

func (r *Request) Patch(url string) (*Response, error)

func (*Request) Post

func (r *Request) Post(url string) (*Response, error)

func (*Request) Put

func (r *Request) Put(url string) (*Response, error)

func (*Request) Reset

func (r *Request) Reset() *Request

func (*Request) SetBasicAuth

func (r *Request) SetBasicAuth(name, passwd string) *Request

func (*Request) SetBody

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

func (*Request) SetForm

func (r *Request) SetForm(form interface{}) *Request

func (*Request) SetJSON

func (r *Request) SetJSON(json interface{}) *Request

func (*Request) SetProxyURL

func (r *Request) SetProxyURL(url string) *Request

func (*Request) SetQueryString

func (r *Request) SetQueryString(qs interface{}) *Request

func (*Request) SetTokenAuth

func (r *Request) SetTokenAuth(token string) *Request

type Response

type Response struct {
	*http.Response
	// contains filtered or unexported fields
}

Response ...

func (*Response) Content

func (resp *Response) Content() ([]byte, error)

Content return Response Body as []byte

func (*Response) JSON

func (resp *Response) JSON() (map[string]interface{}, error)

JSON return Response Body as map[string]interface{}

func (*Response) JSONUnmarshal

func (resp *Response) JSONUnmarshal(data interface{}) error

JSONUnmarshal unmarshal Response Body

func (*Response) OK

func (resp *Response) OK() bool

OK check Response StatusCode < 400 ?

func (*Response) RequestURL

func (resp *Response) RequestURL() (*url.URL, error)

RequestURL return finally request url

func (*Response) Text

func (resp *Response) Text() (string, error)

Text return Response Body as string

type TokenAuth

type TokenAuth struct {
	Token string
}

func (*TokenAuth) RequestHeaderValue

func (a *TokenAuth) RequestHeaderValue() string

type UploadFile

type UploadFile struct {
	Fieldname string
	Filename  string
}

Jump to

Keyboard shortcuts

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