hrq

package module
v0.0.0-...-2cc3803 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2017 License: MIT Imports: 18 Imported by: 0

README

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

Documentation

Overview

Example
package main

import (
	"fmt"

	"github.com/windy-server/hrq"
)

func main() {
	req, _ := hrq.Get("http://example.com")
	res, _ := req.Send()
	s, _ := res.Text()
	fmt.Print(s)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultContentType = applicationFormUrlencoded

DefaultContentType is a default content-type of request.

View Source
var DefaultTimeout = 15

DefaultTimeout is seconds of timeout.

Functions

func MakeURL

func MakeURL(baseURL string, params map[string]string) string

MakeURL makes url

Types

type File

type File struct {
	ContentType string
	FieldName   string
	Name        string
	File        *os.File
}

File is file for multipart/form.

type Request

type Request struct {
	*http.Request
	Timeout time.Duration
	Data    interface{}
	Files   []*File
	// Gzip is a flag to decide whether to compress by gzip or not.
	// (defaut false)
	Gzip bool
}

Request inherits http.Request.

func Delete

func Delete(url string) (req *Request, err error)

Delete make a request whose method is DELETE.

func Get

func Get(url string) (req *Request, err error)

Get make a request whose method is GET.

func Head(url string) (req *Request, err error)

Head make a request whose method is HEAD.

func NewRequest

func NewRequest(method, url string, body io.Reader, timeoutSecond int) (req *Request, err error)

NewRequest make a Request.

func Options

func Options(url string) (req *Request, err error)

Options make a request whose method is OPTIONS.

func Post

func Post(url string, data interface{}) (req *Request, err error)

Post make a request whose method is POST.

func Put

func Put(url string, data interface{}) (req *Request, err error)

Put make a request whose method is PUT.

func (*Request) AcceptGzip

func (r *Request) AcceptGzip() *Request

AcceptGzip is an alias of req.SetHeader("Accept-Encoding", "gzip"). hrq automatically decompress request body.

func (*Request) AddFile

func (r *Request) AddFile(contentType, fieldName, fileName string, file *os.File) *Request

AddFile sets file for multipart/form.

func (*Request) DelHeader

func (r *Request) DelHeader(key string) *Request

DelHeader delete a value of request header by key.

func (*Request) DumpHeader

func (r *Request) DumpHeader() (dump string, err error)

DumpHeader returns a header string.

func (*Request) HeaderValue

func (r *Request) HeaderValue(key string) string

HeaderValue returns a value of request header.

func (*Request) PutCookie

func (r *Request) PutCookie(name, value string) *Request

PutCookie makes a cookie which is setted name and value. It adds a cookie to a request.

func (*Request) Send

func (r *Request) Send() (res *Response, err error)

Send sends request. If method is POST and content-type is application/x-www-form-urlencoded, the request data is urlencoded. If method is POST and content-type is application/json, the request data is converted to json string.

func (*Request) SetApplicationFormUrlencoded

func (r *Request) SetApplicationFormUrlencoded() *Request

SetApplicationFormUrlencoded is an alias of req.SetHeader("Content-Type", "application/x-www-form-urlencoded").

func (*Request) SetApplicationJSON

func (r *Request) SetApplicationJSON() *Request

SetApplicationJSON is an alias of req.SetHeader("Content-Type", "application/json").

func (*Request) SetHeader

func (r *Request) SetHeader(key, value string) *Request

SetHeader sets a value of request header.

func (*Request) SetMultipartFormData

func (r *Request) SetMultipartFormData() *Request

SetMultipartFormData is an alias of req.SetHeader("Content-Type", "multipart/form-data").

func (*Request) SetTimeout

func (r *Request) SetTimeout(timeout int) *Request

SetTimeout sets timeout.

func (*Request) UseGzip

func (r *Request) UseGzip() *Request

UseGzip makes Request.Gzip to true.

func (*Request) WithContext

func (r *Request) WithContext(ctx context.Context) *Request

WithContext sets a context.

type Response

type Response struct {
	*http.Response
	// History is the redirect history.
	History []*http.Request
	// contains filtered or unexported fields
}

Response inherits http.Response.

func (*Response) Content

func (r *Response) Content() (bs []byte, err error)

Content returns response body by byte.

func (*Response) ContentType

func (r *Response) ContentType() string

ContentType returns content-type in response header..

func (*Response) CookieValue

func (r *Response) CookieValue(name string) string

CookieValue returns a cookie value.

func (*Response) CookiesMap

func (r *Response) CookiesMap() map[string]string

CookiesMap returns the response cookies by map.

func (*Response) DumpHeader

func (r *Response) DumpHeader() (dump string, err error)

DumpHeader returns a header string.

func (*Response) Encode

func (r *Response) Encode() (encode string, err error)

Encode returns encode of response body.

func (*Response) HeaderValue

func (r *Response) HeaderValue(name string) string

HeaderValue returns header value.

func (*Response) JSON

func (r *Response) JSON(t interface{}) error

JSON returns unmarshal response body.

func (*Response) Text

func (r *Response) Text() (text string, err error)

Text returns response body by string.

func (*Response) URL

func (r *Response) URL() *url.URL

URL returns a request url.

type Session

type Session struct {
	*http.Client
}

Session is a session.

func NewSession

func NewSession() (s *Session, err error)

NewSession return a session.

func (*Session) CookieValue

func (s *Session) CookieValue(url, name string) string

CookieValue returns a cookie value.

func (*Session) Send

func (s *Session) Send(r *Request) (res *Response, err error)

Send send a request.

Jump to

Keyboard shortcuts

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