httpclient

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2019 License: MIT Imports: 13 Imported by: 0

README

go-httpclient

A more simple and elegant http client in Go

Features

  • Support basic HTTP GET/POST/PUT/DELETE/HEAD in a fluent style
  • Only use Body fluent function to send payload(JSON/string/slice/pointer/map)
  • Basic Authentication
  • Request timeout
  • Debug with customized Logger
  • Receive unmarshal JSON
  • Multipart request
  • Context
  • application/x-www-form-urlencoded
  • todo

Installation

$ go get -u github.com/easonlin404/esrest

Usage

GET/POST/PUT/DELETE


res, err := esrest.New().Get("http://httpbin.org/get").Do()

Add header (Default ContentType is "application/json")

res, err := esrest.New().
		    Get("http://httpbin.org/get").
		    Header("MyHader", "headvalue").
		    Do()

Sending JSON payload use Body chain method same as other:

//JSON struct
res, err := esrest.New().
		    Post("http://httpbin.org/post").
		    Body(struct {
                 		Message string `json:"message"`
                 	}{"ok"}).
		    Do()
//pointer to JSON struct
res, err := esrest.New().
		    Post("http://httpbin.org/post").
		    Body(&struct {
                 		Message string `json:"message"`
                 	}{"ok"}).
		    Do()		    
//slice
res, err := esrest.New().
		    Post("http://httpbin.org/post").
		    Body([]byte(`{"message":"ok"}`)).
		    Do()
		    
//string
res, err := esrest.New().
		    Post("http://httpbin.org/post").
		    Body(string(`{"message":"ok"}`)).
		    Do()
		    
//map
m := map[string]interface{}{
		"message": "ok",
	}
	
res, err := esrest.New().
		    Post("http://httpbin.org/post").
		    Body(m).
		    Do()

Add Query parameter:

res, err := esrest.New().
		    Get("http://httpbin.org/get").
		    Query("Param1", "value").
		    Do()

Receive unmarshal JSON:

json := &struct {
		Message string `json:"message"`
	}{}
res, err := esrest.New().
		    Post("http://httpbin.org/post").
		    DoJson(json)

Basic Authentication:

res, err := esrest.New().
		    BasicAuth("user", "password").
		    Get("http://httpbin.org/get").
		    Do()

Debug:

Print http request and response debug payload at stdout, and you also can use your logger by using Logger chain

mylogger:=log.New(os.Stdout, "", log.LstdFlags)

res, err := esrest.New().
		    Debug(true).
		    Logger(mylogger).  //optional
		    Get("http://httpbin.org/get").
		    Do()

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUrlIsEmpty    = errors.New("url is empty")
	ErrLoggerIsEmpty = errors.New("logger is empty")
)

Functions

This section is empty.

Types

type Builder

type Builder struct {
	Url     string
	Method  string
	Path    string
	Headers map[string]string
	Queries url.Values
	// contains filtered or unexported fields
}

Builder is a object that help to build fluent style API.

func New

func New() *Builder

New returns an new Builder object.

func (*Builder) BasicAuth

func (b *Builder) BasicAuth(username, password string) *Builder

func (*Builder) Body

func (b *Builder) Body(v interface{}) *Builder

Body convert v to []byte and using ad http request body

func (*Builder) ContentType added in v1.0.4

func (b *Builder) ContentType(mimeType string) *Builder

func (*Builder) Debug

func (b *Builder) Debug(debug bool) *Builder

func (*Builder) Delete

func (b *Builder) Delete(url string) *Builder

Delete uses the http DELETE method with provided url and returns Builder.

func (*Builder) Do

func (b *Builder) Do() (*http.Response, error)

Do executes the http request client and returns http.Response and error.

func (*Builder) Get

func (b *Builder) Get(url string) *Builder

Get uses the http GET method with provided url and returns Builder.

func (*Builder) Head

func (b *Builder) Head(url string) *Builder

Head uses the http HEAD method with provided url and returns Builder.

func (*Builder) Header

func (b *Builder) Header(key, value string) *Builder

Header sets http header key with value and returns Builder.

func (*Builder) JsonBody added in v1.0.4

func (b *Builder) JsonBody(v interface{}) *Builder

func (*Builder) Logger

func (b *Builder) Logger(log *log.Logger) *Builder

Logger sets the provided logger and returns Builder.

func (*Builder) Post

func (b *Builder) Post(url string) *Builder

Post uses the http POST method with provided url and returns Builder.

func (*Builder) Put

func (b *Builder) Put(url string) *Builder

Put uses the http PUT method with provided url and returns Builder.

func (*Builder) QueryAdd added in v1.0.1

func (b *Builder) QueryAdd(key, value string) *Builder

QueryAdd adds http QUERY parameter key with value and returns Builder.

func (*Builder) QuerySet added in v1.0.1

func (b *Builder) QuerySet(key string, value string) *Builder

QuerySet sets http QUERY parameter key with value returns Builder.

func (*Builder) StringBody added in v1.0.4

func (b *Builder) StringBody(v string) *Builder

func (*Builder) Timeout

func (b *Builder) Timeout(timeout time.Duration) *Builder

func (*Builder) UnmarshalJson

func (b *Builder) UnmarshalJson(v interface{}) (*http.Response, error)

UnmarshalJson executes the http request client and returns http.Response and error.

func (*Builder) UrlEncodedBody added in v1.0.4

func (b *Builder) UrlEncodedBody(v map[string]string) *Builder

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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