go-httpclient: github.com/ddliu/go-httpclient Index | Files | Directories

package httpclient

import "github.com/ddliu/go-httpclient"

Powerful and easy to use http client

Powerful and easy to use http client


Package Files

default_client.go error.go httpclient.go util.go


const (

Package errors

const (
    VERSION   = "0.6.2"
    USERAGENT = "go-httpclient v" + VERSION

    PROXY_HTTP    = 0
    PROXY_SOCKS4  = 4
    PROXY_SOCKS5  = 5

    // CURL like OPT
    OPT_AUTOREFERER       = 58
    OPT_MAXREDIRS         = 68
    OPT_PROXYTYPE         = 101
    OPT_TIMEOUT           = 13
    OPT_TIMEOUT_MS        = 155
    OPT_COOKIEJAR         = 10082
    OPT_INTERFACE         = 10062
    OPT_PROXY             = 10004
    OPT_REFERER           = 10016
    OPT_USERAGENT         = 10018

    // Other OPT
    OPT_PROXY_FUNC      = 100001
    OPT_DEBUG           = 100002
    OPT_UNSAFE_TLS      = 100004

Constants definations CURL options, see https://github.com/bagder/curl/blob/169fedbdce93ecf14befb6e0e1ce6a2d480252a3/packages/OS400/curl.inc.in


var Begin = defaultClient.Begin
var CONST = map[string]int{
    "OPT_AUTOREFERER":       58,
    "OPT_MAXREDIRS":         68,
    "OPT_PROXYTYPE":         101,
    "OPT_TIMEOUT":           13,
    "OPT_TIMEOUT_MS":        155,
    "OPT_COOKIEJAR":         10082,
    "OPT_INTERFACE":         10062,
    "OPT_PROXY":             10004,
    "OPT_REFERER":           10016,
    "OPT_USERAGENT":         10018,

    "OPT_REDIRECT_POLICY": 100000,
    "OPT_PROXY_FUNC":      100001,
    "OPT_DEBUG":           100002,
    "OPT_UNSAFE_TLS":      100004,

String map of options

var Connect = defaultClient.Connect
var CookieValue = defaultClient.CookieValue
var CookieValues = defaultClient.CookieValues
var Cookies = defaultClient.Cookies
var Defaults = defaultClient.Defaults
var Delete = defaultClient.Delete
var Do = defaultClient.Do
var Get = defaultClient.Get
var Head = defaultClient.Head
var Options = defaultClient.Options
var Patch = defaultClient.Patch
var Post = defaultClient.Post
var PostJson = defaultClient.PostJson
var PostMultipart = defaultClient.PostMultipart
var Put = defaultClient.Put
var PutJson = defaultClient.PutJson
var Trace = defaultClient.Trace
var WithCookie = defaultClient.WithCookie
var WithHeader = defaultClient.WithHeader
var WithHeaders = defaultClient.WithHeaders
var WithOption = defaultClient.WithOption
var WithOptions = defaultClient.WithOptions

func IsRedirectError Uses

func IsRedirectError(err error) bool

Check a redirect error

func IsTimeoutError Uses

func IsTimeoutError(err error) bool

Check a timeout error.

func Option Uses

func Option(o map[string]interface{}) map[int]interface{}

Convert options with string keys to desired format.

type Error Uses

type Error struct {
    Code    int
    Message string

Custom error

func (Error) Error Uses

func (this Error) Error() string

Implement the error interface

type HttpClient Uses

type HttpClient struct {

    // Default headers of this client.
    Headers map[string]string
    // contains filtered or unexported fields

Powerful and easy to use HTTP client.

func NewHttpClient Uses

func NewHttpClient() *HttpClient

Create an HTTP client.

func (*HttpClient) Begin Uses

func (this *HttpClient) Begin() *HttpClient

Begin marks the begining of a request, it's necessary for concurrent requests.

func (*HttpClient) Connect Uses

func (this *HttpClient) Connect(url string, params ...map[string]string) (*Response, error)

The CONNECT request

func (*HttpClient) CookieValue Uses

func (this *HttpClient) CookieValue(url_ string, key string) string

Get cookie value of a specified cookie name.

func (*HttpClient) CookieValues Uses

func (this *HttpClient) CookieValues(url_ string) map[string]string

Get cookie values(k-v map) of the client jar.

func (*HttpClient) Cookies Uses

func (this *HttpClient) Cookies(url_ string) []*http.Cookie

Get cookies of the client jar.

func (*HttpClient) Defaults Uses

func (this *HttpClient) Defaults(defaults Map) *HttpClient

Set default options and headers.

func (*HttpClient) Delete Uses

func (this *HttpClient) Delete(url string, params ...interface{}) (*Response, error)

The DELETE request

func (*HttpClient) Do Uses

func (this *HttpClient) Do(method string, url string, headers map[string]string,
    body io.Reader) (*Response, error)

Start a request, and get the response.

Usually we just need the Get and Post method.

func (*HttpClient) Get Uses

func (this *HttpClient) Get(url string, params ...interface{}) (*Response, error)

The GET request

func (*HttpClient) Head Uses

func (this *HttpClient) Head(url string) (*Response, error)

The HEAD request

func (*HttpClient) Options Uses

func (this *HttpClient) Options(url string, params ...map[string]string) (*Response, error)

The OPTIONS request

func (*HttpClient) Patch Uses

func (this *HttpClient) Patch(url string, params ...map[string]string) (*Response, error)

The PATCH request

func (*HttpClient) Post Uses

func (this *HttpClient) Post(url string, params interface{}) (*Response,

The POST request

With multipart set to true, the request will be encoded as "multipart/form-data".

If any of the params key starts with "@", it is considered as a form file (similar to CURL but different).

func (*HttpClient) PostJson Uses

func (this *HttpClient) PostJson(url string, data interface{}) (*Response, error)

func (*HttpClient) PostMultipart Uses

func (this *HttpClient) PostMultipart(url string, params interface{}) (
    *Response, error)

Post with the request encoded as "multipart/form-data".

func (*HttpClient) Put Uses

func (this *HttpClient) Put(url string, body io.Reader) (*Response, error)

The PUT request

func (*HttpClient) PutJson Uses

func (this *HttpClient) PutJson(url string, data interface{}) (*Response, error)

Put json data

func (*HttpClient) Trace Uses

func (this *HttpClient) Trace(url string, params ...map[string]string) (*Response, error)

The TRACE request

func (*HttpClient) WithCookie Uses

func (this *HttpClient) WithCookie(cookies ...*http.Cookie) *HttpClient

Specify cookies of the current request.

func (*HttpClient) WithHeader Uses

func (this *HttpClient) WithHeader(k string, v string) *HttpClient

Temporarily specify a header of the current request.

func (*HttpClient) WithHeaders Uses

func (this *HttpClient) WithHeaders(m map[string]string) *HttpClient

Temporarily specify multiple headers of the current request.

func (*HttpClient) WithOption Uses

func (this *HttpClient) WithOption(k int, v interface{}) *HttpClient

Temporarily specify an option of the current request.

func (*HttpClient) WithOptions Uses

func (this *HttpClient) WithOptions(m Map) *HttpClient

Temporarily specify multiple options of the current request.

type Map Uses

type Map map[interface{}]interface{}

Map is a mixed structure with options and headers

type Response Uses

type Response struct {

Thin wrapper of http.Response(can also be used as http.Response).

func (*Response) ReadAll Uses

func (this *Response) ReadAll() ([]byte, error)

Read response body into a byte slice.

func (*Response) ToString Uses

func (this *Response) ToString() (string, error)

Read response body into string.



Package httpclient imports 18 packages (graph) and is imported by 22 packages. Updated 2019-03-08. Refresh now. Tools for package owners.