38elements / hrq-1

Http client like requests for Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

hrq

build status GoDoc Go Report Card

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)
}

Table of contents

Installation

dep ensure -add github.com/windy-server/hrq

or

go get -u github.com/windy-server/hrq

Usage

Request

hrq.Request inherits http.Request.

Get

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)

Post

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)

Response

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)

Header

req, _ := hrq.Get("http://example.com")
req.SetHeader("abc", "efg")
res, _ := req.Send()
v := res.HeaderValue("foo")
fmt.Print(v)

Cookie

req, _ := hrq.Get("http://example.com")
req.PutCookie("abc", "efg")
res, _ := req.Send()
v := res.CookieValue("foo")
cm := res.CookiesMap()

Timeout

req, _ := hrq.Get("http://example.com")
// This sets requset timeout to 30 seconds.
// (Default timeout is 15 seconds.)
req.SetTimeout(30)
res, _ := req.Send()

File

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()

JSON

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)

History

req, _ := hrq.Get("http://example.com")
res, _ := req.Send()
// The redirect history by http.Request slice
history := req.History
// The recent request
res.Request

Gzip

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

session, _ := hrq.NewSession()
req, _ := hrq.Get("http://example.com")
res, _ := session.Send(req)

About

Http client like requests for Go

License:MIT License


Languages

Language:Go 100.0%