xhttp

package module
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2024 License: MIT Imports: 10 Imported by: 0

README

XHttp

XHttp is a http client written by golang

GoDoc Build Status

feature 特性
easy to use 使用简单
chain expression 链式表达式
set your request method/header/param/body 支持请求方法/头/参数/体
get json/string/json's key from response 支持获取json/string/json字段
custom timeout 支持自定义超时
no "defer resp.Body.Close()" 免"defer resp.Body.Close()"

Usage

package main

import (
	"fmt"

	"github.com/adwpc/xhttp"
	"github.com/buger/jsonparser"
)

type Json struct {
	Args    map[string]string
	Headers map[string]string
	Origin  string
	Url     string
}

func simpleGetRespToString() {
	fmt.Println("--------simpleGetRespToString-----------")
	ip, err := xhttp.New().Get().RespToString("http://httpbin.org/get")

	if err != nil {
		panic(err)
	}
	fmt.Println(ip)
	v, _, _, _ := jsonparser.Get([]byte(ip), "headers", "Host")
	fmt.Println(string(v))
}

func simpleGetRespJsonKey() {
	fmt.Println("--------simpleGetRespJsonKey-----------")
	host, err := xhttp.New().Get().RespGetJsonKey("http://httpbin.org/get", "origin")

	if err != nil {
		panic(err)
	}
	fmt.Println(string(host))
}

func customGetRespToJson() {
	fmt.Println("--------customGetRespToJson-----------")
	var j Json
	err := xhttp.NewWithTimeout(5000, 5000, 10000).Get().AddHeader("a", "b").AddParam("c", "d").SetBody("{\"e\":\"f\"}").RespToJson("http://httpbin.org/get", &j)

	if err != nil {
		panic(err)
	}
	fmt.Println(j)
}

func multiCustomPostRespToJson() {
	fmt.Println("--------multiCustomPostRespToJson-----------")
	headers := make(map[string]string)
	headers["a"] = "b"
	headers["c"] = "d"

	params := make(map[string]string)
	params["q"] = "ip"

	var j Json
	err := xhttp.New().Post().AddHeaders(headers).AddParams(params).SetBody("{\"e\":\"f\"}").RespToJson("http://httpbin.org/post", &j)

	if err != nil {
		panic(err)
	}
	fmt.Println(j)
}
func main() {
	simpleGetRespToString()
	simpleGetRespJsonKey()
	customGetRespToJson()
	multiCustomPostRespToJson()
}

Documentation

Index

Constants

View Source
const (
	ErrorDefault = -iota - 1 //-1
	ErrorInvalidUrl
	ErrorInvalidMethod
)

Variables

This section is empty.

Functions

This section is empty.

Types

type XHttp

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

func New

func New() *XHttp

New with default options

func NewWithOption added in v0.2.0

func NewWithOption(connectTimeout, responseHeaderTimeout, totalTimeout int64, proxy string) *XHttp

NewWithOption

func (*XHttp) AddHeader

func (c *XHttp) AddHeader(k, v string) *XHttp

add a http header

func (*XHttp) AddHeaders

func (c *XHttp) AddHeaders(headerMap map[string]string) *XHttp

add some http header

func (*XHttp) AddParam

func (c *XHttp) AddParam(k, v string) *XHttp

set the http uri param

func (*XHttp) AddParams

func (c *XHttp) AddParams(paramMap map[string]string) *XHttp

add some http params

func (*XHttp) Get

func (c *XHttp) Get() *XHttp

set the http method to get

func (*XHttp) GetRestBody added in v0.2.1

func (c *XHttp) GetRestBody(url string) (*http.Response, error)

get a http response body warning: you must close body at last

func (*XHttp) Method

func (c *XHttp) Method(m string) *XHttp

set the http method

func (*XHttp) Post

func (c *XHttp) Post() *XHttp

set the http method to post

func (*XHttp) RespGetJsonKey

func (c *XHttp) RespGetJsonKey(url string, keys ...string) ([]byte, error)

get a json's value from http request

func (*XHttp) RespToJson

func (c *XHttp) RespToJson(url string, j interface{}) error

get all data from http request

func (*XHttp) RespToJsonByKeys

func (c *XHttp) RespToJsonByKeys(url string, j interface{}, keys ...string) error

get all data from http request

func (*XHttp) RespToString

func (c *XHttp) RespToString(url string) (string, error)

get all data from http request

func (*XHttp) SetBody

func (c *XHttp) SetBody(b string) *XHttp

set the http body

func (*XHttp) SetJsonBody

func (c *XHttp) SetJsonBody(b interface{}) *XHttp

set the http body by interface make sure marshal ok

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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