Http client like requests in Go
import (
"fmt"
"github.com/windy-server/hrq"
)
func main() {
req, _ := hrq.Get("http://example.com")
res, _ := req.Send()
s, _ := res.Text()
fmt.Print(s)
}
dep ensure -add github.com/windy-server/hrq
or
go get -u github.com/windy-server/hrq
hrq.Request inherits http.Request.
params := map[string]string{
"foo": "123",
"bar": "456",
}
// http://example.com?foo=123&bar=456
url := hrq.MakeURL("http://example.com", params)
req, _ := hrq.Get(url)
res, _ := req.Send()
s, _ := res.Text()
fmt.Print(s)
data := map[string]string{
"foo": "123",
"bar": "456",
}
req, _ := hrq.Post("http://example.com", data)
// When Content-Type is "application/x-www-form-urlencoded"(It is default),
// the request data is urlencoded.
// The request data must be a map[string]string instance.
// When Content-Type is "application/json",
// the request data is converted to json string.
// When Content-Type is "multipart/form-data",
// the request data is converted to fields.
res, _ := req.SetApplicationFormUrlencoded().Send()
s, _ := res.Text()
fmt.Print(s)
hrq.Response inherits http.Response.
req, _ := hrq.Get("http://example.com")
res, _ := req.Send()
// get request body by byte
b, _ := res.Content()
// get request body by string
s, _ := res.Text()
fmt.Print(s)
req, _ := hrq.Get("http://example.com")
req.SetHeader("abc", "efg")
res, _ := req.Send()
v := res.HeaderValue("foo")
fmt.Print(v)
req, _ := hrq.Get("http://example.com")
req.PutCookie("abc", "efg")
res, _ := req.Send()
v := res.CookieValue("foo")
cm := res.CookiesMap()
req, _ := hrq.Get("http://example.com")
// This sets requset timeout to 30 seconds.
// (Default timeout is 15 seconds.)
req.SetTimeout(30)
res, _ := req.Send()
data := map[string]string{
"foo": "123",
"bar": "456",
}
req, _ := hrq.Post("http://example.com", data)
// When Content-Type is "multipart/form-data",
// the request data is converted to fields.
req.SetMultipartFormData()
file, _ := os.Open("foo.gif")
req.AddFile("image/gif", "foo", "foo.gif", file)
res, _ := req.Send()
data := map[string]string{
"foo": "123",
"bar": "456",
}
req, _ := hrq.Post("http://example.com", data)
// When Content-Type is "application/json",
// the request data is converted to json string.
req.SetApplicationJSON()
res, _ := req.Send()
var result map[string]string
err := res.JSON(&result)
req, _ := hrq.Get("http://example.com")
res, _ := req.Send()
// The redirect history by http.Request slice
history := req.History
// The recent request
res.Request
data := map[string]string{
"foo": "123",
"bar": "456",
}
req, _ := hrq.Post("http://example.com", data)
// You can send a request compressed by gzip.
req.UseGzip()
res, _ := req.SetApplicationJSON().Send()
session, _ := hrq.NewSession()
req, _ := hrq.Get("http://example.com")
res, _ := session.Send(req)