quest is an elegant HTTP networking library written in Go. Alamofire inspired.
- Chainable Request / Response methods
- URL / JSON / Parameter Encoding
- Progress Tracking
- Upload File / Data / Stream
- Download using Request
- Authentication
- Timeout, defaults to 30 * time.Second
- cURL Debug Output
- TLS config
- Support Short APIS
Get
,Post
,Patch
,Put
,Delete
- Pipe Stream to other request
- Download Resume data
- More Errors Output
- HTTP Response Validation
- Comprehensive Unit Test Coverage
- Complete Documentation
import "github.com/go-libs/quest"
q, err := quest.Request("GET", "http://httpbin.org/get")
q, err := quest.Request(quest.GET, "http://httpbin.org/get")
q.
Response(func(req *http.Request, res *http.Response, data *bytes.Buffer, e error) {
log.Println(req)
log.Println(res)
log.Println(data)
log.Println(err)
})
Built-in Response Methods
Response(func(*http.Request, *http.Response, *bytes.Buffer, error))
ResponseBytes(func(*http.Request, *http.Response, []byte, error))
ResponseString(func(*http.Request, *http.Response, string, error))
ResponseJSON(f interface{})
,f
Must befunc
func(req *http.Request, res *http.Response, data *JSONStruct, e error)
func(req *http.Request, res *http.Response, data JSONStruct, e error)
q, _ := quest.Request(quest.GET, "http://httpbin.org/get")
q.ResponseString(func(req *http.Request, res *http.Response, data string, e error) {
log.Println(data)
})
type DataStruct struct {
Headers map[string]string
Origin string
}
q, _ := quest.Request(quest.GET, "http://httpbin.org/get")
q.ResponseJSON(func(req *http.Request, res *http.Response, data DataStruct, e error) {
log.Println(data)
})
q.ResponseJSON(func(req *http.Request, res *http.Response, data *DataStruct, e error) {
log.Println(data)
})
Response handlers can even be chained:
q, _ := quest.Request(quest.GET, "http://httpbin.org/get")
q.
ResponseString(func(req *http.Request, res *http.Response, data string, e error) {
log.Println(data)
}).
ResponseJSON(func(req *http.Request, res *http.Response, data *DataStruct, e error) {
log.Println(data)
})
OPTIONS
GET
HEAD
POST
PUT
PATCH
DELETE
TRACE
CONNECT
type Options struct {
Foo string `url:"foo"`
}
q, _ := quest.Request(quest.GET, "http://httpbin.org/get")
q.Query(Options{"bar"})
// http://httpbin.org/get?foo=bar
type PostParameters struct {
Foo []int `json:"foo,omitempty"`
Bar map[string]string `json:"bar,omitempty"`
}
parameters := PostParameters{
"foo": []int{1, 2, 3},
"bar": map[string]string{"baz": "qux"},
}
type DataStruct struct {
Headers map[string]string
Origin string
Json PostParameters `json:"json,omitempty"`
}
type OtherDataStruct struct {
Headers map[string]string
Origin string
}
q, _ := quest.Request(quest.POST, "http://httpbin.org/post")
q.Encoding("JSON").
Parameters(¶meters).
ResponseJSON(func(req *http.Request, res *http.Response, data *DataStruct, e error) {
log.Println(data)
}).
ResponseJSON(func(req *http.Request, res *http.Response, data OtherDataStruct, e error) {
log.Println(data)
})
q, _ := quest.Download(quest.GET, "http://httpbin.org/stream/100", "stream.log")
q.Do()
destination := "tmp/stream.log"
q, _ := quest.Download(quest.GET, "http://httpbin.org/bytes/1024", destination)
q.
Progress(func(bytesRead, totalBytesRead, totalBytesExpectedToRead int64) {
log.Println(bytesRead, totalBytesRead, totalBytesExpectedToRead)
}).Do()
destination, _ := os.Create("tmp/stream2.log")
q, _ := quest.Download(quest.GET, "http://httpbin.org/bytes/10240", destination)
q.
Progress(func(current, total, expected int64) {
log.Println(current, total, expected)
}).Response(func(request *http.Request, response *http.Response, data *bytes.Buffer, err error) {
log.Println(data.Len())
})
- File
- Data
- Stream
q, _: = quest.Upload(quest.POST, "http://httpbin.org/post", map[string]string{"stream": "tmp/stream.log"})
q.Do()
stream2, _ := os.Open("tmp/stream2.log")
stream3 := bytes.NewBufferString(`Hello Quest!`)
data := map[string]interface{}{
"stream1": "tmp/stream.log", // filepath or filename
"stream2": stream2, // *os.File
"stream3": stream3, // io.Reader, filename is fieldname `stream3`
}
q, _ := quest.Upload(quest.POST, "http://httpbin.org/post", data)
q.
Progress(func(current, total, expected int64) {
log.Println(current, total, expected)
}).Response(func(req *http.Request, res *http.Response, data *bytes.Buffer, err error) {
log.Println(data.Len())
})
type Auth struct {
User string
Passwd string
Authenticated bool
}
user := "user"
passwd := "password"
q, _ := quest.Request(quest.GET, "https://httpbin.org/basic-auth/"+user+"/"+passwd)
q.Authenticate(user, passwd).
ResponseJSON(func(_ *http.Request, _ *http.Response, data Auth, _ error) {
log.Println(data)
}).Do()
s := time.Duration(3 * time.Second)
q, _ := quest.Request(quest.GET, "https://httpbin.org/delay/5")
q.Timeout(s).Do()
q, _ := Request(GET, "http://httpbin.org/cookies")
log.Println(q.Println())
c := &http.Cookie{}
c.Name = "k"
c.Value = "v"
q, _ := Request(GET, "http://httpbin.org/cookies")
q.Query(&queryParams)
q.Cookie(c)
log.Println(q.DebugPrintln())
q, _ := Request(GET, "http://httpbin.org/get")
b1, err := q.Bytes()
s1, err := q.String()
type DataStruct struct {
Headers map[string]string
Origin string
}
v := DataStruct{}
err := q.JSON(&v)
t := &tls.Config{}
q, _ := Request(GET, "http://httpbin.org/get")
q.TLSConfig(t)
q, _ = Get("http://httpbin.org/get")
MIT