ghttpclient

package module
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2022 License: MIT Imports: 19 Imported by: 0

README

GHttpClient

A method chaining HTTP Client for Golang with simple methods

Simple Methods

Get, Post, PostJson, PostForm, Put, PutJson, Patch, Delete, Options

Examples

package main

import (
	"fmt"
	"github.com/panwenbin/ghttpclient"
	"io/ioutil"
	"log"
)

func main() {
    response, err := ghttpclient.Get("http://www.panwenbin.com/", nil).Response()
    if err != nil {
        log.Fatal(err)
    }

    body, err := ghttpclient.ReadBodyClose(response)
    fmt.Printf("%s", body)
}
    body, err := ghttpclient.Get("http://www.panwenbin.com/", nil).ReadBodyClose()
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("%s", body)
headers := header.GHttpHeader{}
headers.UserAgent("ghttpclient")
response, err := ghttpclient.NewClient().
    Url("http://www.panwenbin.com/").
    Headers(headers).
    Get().Response()

API Reference: https://godoc.org/github.com/panwenbin/ghttpclient

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Debug bool
View Source
var Transport *http.Transport

Transport is the default transport for ghttpclient

Functions

func GzipBody added in v0.3.5

func GzipBody(body io.Reader) *bytes.Buffer

GzipBody encodes the body with gzip

func ReadBodyClose added in v0.2.0

func ReadBodyClose(response *http.Response) ([]byte, error)

ReadBodyClose fetches the response Body, then close the Body supports gzip content-type

func ReadJsonClose added in v0.2.0

func ReadJsonClose(response *http.Response, v interface{}) error

ReadJsonClose fetches the response Body and try to decode as a json, then close the Body

func ResetTransport added in v0.4.2

func ResetTransport()

ResetTransport resets the Transport

func SslSkipVerify added in v0.4.0

func SslSkipVerify()

SslSkipVerify sets whether or not skipping ssl verify

func TryUTF8ReadBodyClose added in v0.2.4

func TryUTF8ReadBodyClose(response *http.Response) ([]byte, error)

TryUTF8ReadBodyClose tries to transfer the body bytes to utf-8 bytes when the body bytes is not in utf-8 encoding

Types

type GHttpClient

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

GHttpClient is a Method chaining HTTP Client which is based on net/http.Client NewClient => set attributes of a request => do the request with an action(Get, Post...)

func Delete

func Delete(url string, httpHeader header.GHttpHeader) *GHttpClient

Send a Request with DELETE method

func Get

func Get(url string, httpHeader header.GHttpHeader) *GHttpClient

Send a Request with GET method

func NewClient

func NewClient() *GHttpClient

NewClient Returns a new GHttpClient

func Options

func Options(url string, httpHeader header.GHttpHeader) *GHttpClient

Send a Request with OPTIONS method

func Patch

func Patch(url string, body io.Reader, httpHeader header.GHttpHeader) *GHttpClient

Send a Request with PATCH method

func Post

func Post(url string, body io.Reader, httpHeader header.GHttpHeader) *GHttpClient

Send a Request with POST method

func PostForm

func PostForm(url string, data url.Values, httpHeader header.GHttpHeader) *GHttpClient

Send a Request as a form with POST method

func PostJson

func PostJson(url string, jsonBytes []byte, httpHeader header.GHttpHeader) *GHttpClient

Send a Request as a json with POST Method

func Put

func Put(url string, body io.Reader, httpHeader header.GHttpHeader) *GHttpClient

Send a Request with PUT method

func PutJson

func PutJson(url string, jsonBytes []byte, httpHeader header.GHttpHeader) *GHttpClient

Send a Request as a json with PUT method

func (*GHttpClient) Body

func (g *GHttpClient) Body(body io.Reader) *GHttpClient

Body sets the body of the request

func (*GHttpClient) ContentType added in v0.2.0

func (g *GHttpClient) ContentType(contentType string) *GHttpClient

ContentType sets the Content-Type header

func (*GHttpClient) CookieJar added in v0.3.1

func (g *GHttpClient) CookieJar(cookieJar http.CookieJar) *GHttpClient

CookieJar sets the cookie jar of the request

func (*GHttpClient) Debug added in v0.3.0

func (g *GHttpClient) Debug(debug bool) *GHttpClient

Debug sets debug to the value of param

func (*GHttpClient) DebugOn added in v0.3.0

func (g *GHttpClient) DebugOn() *GHttpClient

DebugOn sets debug to true

func (*GHttpClient) Delete

func (g *GHttpClient) Delete() *GHttpClient

Delete sends the Request with DELETE method

func (*GHttpClient) DeleteWithContext added in v0.4.0

func (g *GHttpClient) DeleteWithContext(ctx context.Context) *GHttpClient

DeleteWithContext

func (*GHttpClient) Get

func (g *GHttpClient) Get() *GHttpClient

Get sends the Request with GET method

func (*GHttpClient) GetWithContext added in v0.4.0

func (g *GHttpClient) GetWithContext(ctx context.Context) *GHttpClient

GetWithContext

func (*GHttpClient) Head

func (g *GHttpClient) Head() *GHttpClient

Head sends the Request with HEAD method

func (*GHttpClient) HeadWithContext added in v0.4.0

func (g *GHttpClient) HeadWithContext(ctx context.Context) *GHttpClient

HeadWithContext

func (*GHttpClient) Header

func (g *GHttpClient) Header(headerKey, headerValue string) *GHttpClient

Header sets a header

func (*GHttpClient) Headers

func (g *GHttpClient) Headers(httpHeader header.GHttpHeader) *GHttpClient

Headers sets a group of headers

func (*GHttpClient) LogDebug added in v0.3.0

func (g *GHttpClient) LogDebug(flag string)

func (*GHttpClient) NoRedirect added in v0.3.3

func (g *GHttpClient) NoRedirect(noFollow bool) *GHttpClient

NoRedirect sets whether or not to stop following redirects

func (*GHttpClient) Options

func (g *GHttpClient) Options() *GHttpClient

Options sends the Request with OPTIONS method

func (*GHttpClient) OptionsWithContext added in v0.4.0

func (g *GHttpClient) OptionsWithContext(ctx context.Context) *GHttpClient

OptionsWithContext

func (*GHttpClient) Patch

func (g *GHttpClient) Patch() *GHttpClient

Patch sends the Request with PATCH method

func (*GHttpClient) PatchWithContext added in v0.4.0

func (g *GHttpClient) PatchWithContext(ctx context.Context) *GHttpClient

Patch sends the Request with PATCH method

func (*GHttpClient) Post

func (g *GHttpClient) Post() *GHttpClient

Post sends the Request with POST method

func (*GHttpClient) PostWithContext added in v0.4.0

func (g *GHttpClient) PostWithContext(ctx context.Context) *GHttpClient

PostWithContext

func (*GHttpClient) Proxy added in v0.4.3

func (g *GHttpClient) Proxy(proxyFunc func(req *http.Request) (*url.URL, error)) *GHttpClient

Proxy sets a proxyFunc

func (*GHttpClient) Put

func (g *GHttpClient) Put() *GHttpClient

Put sends the Request with PUT method

func (*GHttpClient) PutWithContext added in v0.4.0

func (g *GHttpClient) PutWithContext(ctx context.Context) *GHttpClient

PutWithContext

func (*GHttpClient) ReadBodyClose added in v0.3.0

func (g *GHttpClient) ReadBodyClose() ([]byte, error)

ReadBodyClose fetches the response Body, then close the Body supports gzip content-type

func (*GHttpClient) ReadJsonClose added in v0.3.0

func (g *GHttpClient) ReadJsonClose(v interface{}) error

ReadJsonClose fetches the response Body and try to decode as a json, then close the Body

func (*GHttpClient) Response added in v0.3.0

func (g *GHttpClient) Response() (*http.Response, error)

Response returns http.Response and error

func (*GHttpClient) SslSkipVerify

func (g *GHttpClient) SslSkipVerify(skip bool) *GHttpClient

SslSkipVerify sets whether or not skipping ssl verify Deprecated: use package SslSkipVerify instead

func (*GHttpClient) Timeout

func (g *GHttpClient) Timeout(timeout time.Duration) *GHttpClient

Timeout sets a timeout to the http client

func (*GHttpClient) TryUTF8ReadBodyClose added in v0.3.0

func (g *GHttpClient) TryUTF8ReadBodyClose() ([]byte, error)

TryUTF8ReadBodyClose tries to transfer the body bytes to utf-8 bytes when the body bytes is not in utf-8 encoding

func (*GHttpClient) Url

func (g *GHttpClient) Url(url string) *GHttpClient

Url sets the url to request to

func (*GHttpClient) UserAgent added in v0.2.0

func (g *GHttpClient) UserAgent(userAgent string) *GHttpClient

UserAgent sets the User-Agent header

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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