README
¶
XHttp
XHttp is a http client written by golang
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
- type XHttp
- func (c *XHttp) AddHeader(k, v string) *XHttp
- func (c *XHttp) AddHeaders(headerMap map[string]string) *XHttp
- func (c *XHttp) AddParam(k, v string) *XHttp
- func (c *XHttp) AddParams(paramMap map[string]string) *XHttp
- func (c *XHttp) Get() *XHttp
- func (c *XHttp) GetRestBody(url string) (*http.Response, error)
- func (c *XHttp) Method(m string) *XHttp
- func (c *XHttp) Post() *XHttp
- func (c *XHttp) RespGetJsonKey(url string, keys ...string) ([]byte, error)
- func (c *XHttp) RespToJson(url string, j interface{}) error
- func (c *XHttp) RespToJsonByKeys(url string, j interface{}, keys ...string) error
- func (c *XHttp) RespToString(url string) (string, error)
- func (c *XHttp) SetBody(b string) *XHttp
- func (c *XHttp) SetJsonBody(b interface{}) *XHttp
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 NewWithOption ¶ added in v0.2.0
func NewWithOption(connectTimeout, responseHeaderTimeout, totalTimeout int64, proxy string, skipTLS bool) *XHttp
NewWithOption
func (*XHttp) AddHeaders ¶
add some http header
func (*XHttp) GetRestBody ¶ added in v0.2.1
get a http response body warning: you must close body at last
func (*XHttp) RespGetJsonKey ¶
get a json's value from http request
func (*XHttp) RespToJson ¶
get all data from http request
func (*XHttp) RespToJsonByKeys ¶
get all data from http request
func (*XHttp) RespToString ¶
get all data from http request
func (*XHttp) SetJsonBody ¶
set the http body by interface make sure marshal ok
Click to show internal directories.
Click to hide internal directories.