httpclient

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2019 License: MIT Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

httpclient

golang net/http wrapper

HttpClient

Examples
client := httpclient.NewHttpClient("https", "jsonplaceholder.typicode.com", 5*time.Second)
	statusCode, res, err := client.Request(http.MethodGet, "/posts", nil, nil)

	fmt.Println(statusCode, res, err) // 200 [<bytes>] <nil>

JsonClient

Examples

Post request

client := httpclient.NewJsonClient("https", "jsonplaceholder.typicode.com", 5*time.Second)
	statusCode, res, err := client.Post("/posts",
		&map[string]interface{}{
			"test": 101,
		}, nil)

	if r, ok := res.(map[string]interface{}); ok {
		fmt.Println(statusCode, r, err)
	}

Returning json object

client := httpclient.NewJsonClient("https", "jsonplaceholder.typicode.com", 5*time.Second)
	statusCode, res, err := client.Get("/posts/1", nil, nil)

	if r, ok := res.(map[string]interface{}); ok {
		fmt.Println(statusCode, r, err) // 200 <map> <nil>
	}

Returning json array

client := httpclient.NewJsonClient("https", "jsonplaceholder.typicode.com", 5*time.Second)
	statusCode, res, err := client.Get("/posts", nil, nil)

	if r, ok := res.([]interface{}); ok {
		fmt.Println(statusCode, r, err) // 200 <slice> <nil>
	}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewHttpClient

func NewHttpClient(scheme string, host string, timeout time.Duration) *httpClient

func NewJsonClient

func NewJsonClient(scheme string, host string, timeout time.Duration) *jsonClient

Types

type HttpClient

type HttpClient interface {
	RequestNoBody(method string, path string, param *map[string]string, header *map[string]string) (statusCode int, body []byte, err error)
	Request(method string, path string, param []byte, header *map[string]string) (statusCode int, body []byte, err error)
}

type JsonClient

type JsonClient interface {
	Delete(path string, param *map[string]string, header *map[string]string) (statusCode int, res interface{}, err error)
	Get(path string, param *map[string]string, header *map[string]string) (statusCode int, res interface{}, err error)
	Patch(path string, param *map[string]interface{}, header *map[string]string) (statusCode int, res interface{}, err error)
	Post(path string, param *map[string]interface{}, header *map[string]string) (statusCode int, res interface{}, err error)
	Put(path string, param *map[string]interface{}, header *map[string]string) (statusCode int, res interface{}, err error)
}

Jump to

Keyboard shortcuts

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