request

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2016 License: MIT, MIT Imports: 16 Imported by: 1

README

request

Build Status Coverage Status GoDoc

Go HTTP Requests for Humans™. Inspired by Python-Requests.

Installation

go get -u github.com/mozillazg/request

Documentation

API documentation can be found here: https://godoc.org/github.com/mozillazg/request

Usage

import (
	"github.com/mozillazg/request"
)

GET:

c := new(http.Client)
req := request.NewRequest(c)
resp, err := req.Get("http://httpbin.org/get")
j, err := resp.Json()
defer resp.Body.Close()  // Don't forget close the response body

POST:

req.Data = map[string]string{
	"key": "value",
	"a":   "123",
}
resp, err := req.Post("http://httpbin.org/post")

Cookies:

req.Cookies = map[string]string{
	"key": "value",
	"a":   "123",
}
resp, err := req.Get("http://httpbin.org/cookies")

Headers:

req.Headers = map[string]string{
	"Accept-Encoding": "gzip,deflate,sdch",
	"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
}
resp, err := req.Get("http://httpbin.org/get")

Files:

f, err := os.Open("test.txt")
req.Files = []request.FileField{
	request.FileField{"file", "test.txt", f},
}
resp, err := req.Post("http://httpbin.org/post")

Json:

req.Json = map[string]string{
	"a": "A",
	"b": "B",
}
resp, err := req.Post("http://httpbin.org/post")
req.Json = []int{1, 2, 3}
resp, err = req.Post("http://httpbin.org/post")

Proxy:

req.Proxy = "http://127.0.0.1:8080"
// req.Proxy = "https://127.0.0.1:8080"
// req.Proxy = "socks5://127.0.0.1:57341"
resp, err := req.Get("http://httpbin.org/get")

or https://github.com/mozillazg/request/tree/develop/_example/proxy

HTTP Basic Authentication:

req.BasicAuth = request.BasicAuth{"user", "passwd"}
resp, err := req.Get("http://httpbin.org/basic-auth/user/passwd")

Documentation

Overview

Go HTTP Requests for Humans™.

GET Request:

c := &http.Client{}
req := request.NewRequest(c)
resp, err := req.Get("http://httpbin.org/get")
defer resp.Body.Close()  // **Don't forget close the response body**
j, err := resp.Json()

POST Request:

req.Data = map[string]string{
	"key": "value",
	"a":   "123",
}
resp, err := req.Post("http://httpbin.org/post")

Custom Cookies:

req.Cookies = map[string]string{
	"key": "value",
	"a":   "123",
}
resp, err := req.Get("http://httpbin.org/cookies")

Custom Headers:

req.Headers = map[string]string{
	"Accept-Encoding": "gzip,deflate,sdch",
	"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
}
resp, err := req.Get("http://httpbin.org/get")

Upload Files:

f, err := os.Open("test.txt")
req.Files = []request.FileField{
	request.FileField{"file", "test.txt", f},
}
resp, err := req.Post("http://httpbin.org/post")

Json Body:

req.Json = map[string]string{
	"a": "A",
	"b": "B",
}
resp, err := req.Post("http://httpbin.org/post")
req.Json = []int{1, 2, 3}
resp, err = req.Post("http://httpbin.org/post")

others body:

// not set Content-Type
req.Body = strings.NewReader("<xml><a>abc</a></xml")
resp, err := req.Post("http://httpbin.org/post")

// form
req.Body = strings.NewReader("a=1&b=2")
req.Headers = map[string]string{
	"Content-Type": request.DefaultContentType,
}
resp, err = req.Post("http://httpbin.org/post")

Proxy:

req.Proxy = "http://127.0.0.1:8080"
// req.Proxy = "https://127.0.0.1:8080"
// req.Proxy = "socks5://127.0.0.1:57341"
resp, err := req.Get("http://httpbin.org/get")

HTTP Basic Authentication:

req.BasicAuth = request.BasicAuth{"user", "passwd"}
resp, err := req.Get("http://httpbin.org/basic-auth/user/passwd")

Need more control?

You can setup req.Client(you know, it's an &http.Client), for example: set timeout

timeout := time.Duration(1 * time.Second)
req.Client.Timeout = timeout
req.Get("http://httpbin.org/get")

Index

Constants

View Source
const Version = "0.5.0"

Variables

View Source
var DefaultContentType = "application/x-www-form-urlencoded; charset=utf-8"

Default Content-Type Header for form body

View Source
var DefaultHeaders = map[string]string{
	"Connection":      "keep-alive",
	"Accept-Encoding": "gzip, deflate",
	"Accept":          "*/*",
	"User-Agent":      DefaultUserAgent,
}
View Source
var DefaultJsonType = "application/json; charset=utf-8"

Default Content-Type Header for json body

View Source
var DefaultRedirectLimit = 10
View Source
var DefaultUserAgent = "go-request/" + Version

Functions

This section is empty.

Types

type Args

type Args struct {
	Client    *http.Client
	Headers   map[string]string
	Cookies   map[string]string
	Data      map[string]string
	Params    map[string]string
	Files     []FileField
	Json      interface{}
	Proxy     string
	BasicAuth BasicAuth
	Body      io.Reader
}

func NewArgs

func NewArgs(c *http.Client) *Args

type BasicAuth

type BasicAuth struct {
	Username string
	Password string
}

type FileField

type FileField struct {
	FieldName string
	FileName  string
	File      io.Reader
}

type Request

type Request struct {
	*Args
}

func NewRequest

func NewRequest(c *http.Client) *Request

func (*Request) Delete

func (req *Request) Delete(url interface{}) (resp *Response, err error)

url can be string or *url.URL or ur.URL

func (*Request) Get

func (req *Request) Get(url interface{}) (resp *Response, err error)

url can be string or *url.URL or ur.URL

func (*Request) Head

func (req *Request) Head(url interface{}) (resp *Response, err error)

url can be string or *url.URL or ur.URL

func (*Request) Options

func (req *Request) Options(url interface{}) (resp *Response, err error)

url can be string or *url.URL or ur.URL

func (*Request) Patch

func (req *Request) Patch(url interface{}) (resp *Response, err error)

url can be string or *url.URL or ur.URL

func (*Request) Post

func (req *Request) Post(url interface{}) (resp *Response, err error)

url can be string or *url.URL or ur.URL

func (*Request) PostForm

func (req *Request) PostForm(url interface{}, data interface{}) (resp *Response, err error)

url can be string or *url.URL or ur.URL

data can be map[string]string or map[string][]string or string or io.Reader

data := map[string]string{
	"a": "1",
	"b": "2",
}

data := map[string][]string{
	"a": []string{"1", "2"},
	"b": []string{"2", "3"},
}

data : = "a=1&b=2"

data : = strings.NewReader("a=1&b=2")

func (*Request) Put

func (req *Request) Put(url interface{}) (resp *Response, err error)

url can be string or *url.URL or ur.URL

func (*Request) Reset

func (req *Request) Reset()

Reset all fields to default values

type Response

type Response struct {
	*http.Response
	// contains filtered or unexported fields
}

func Delete

func Delete(url string, a *Args) (resp *Response, err error)

Delete issues a DELETE to the specified URL.

Caller should close resp.Body when done reading from it.

func Get

func Get(url string, a *Args) (resp *Response, err error)

Get issues a GET to the specified URL.

Caller should close resp.Body when done reading from it.

func Head(url string, a *Args) (resp *Response, err error)

Head issues a HEAD to the specified URL.

Caller should close resp.Body when done reading from it.

func Options

func Options(url string, a *Args) (resp *Response, err error)

Options issues a OPTIONS to the specified URL.

Caller should close resp.Body when done reading from it.

func Patch

func Patch(url string, a *Args) (resp *Response, err error)

Patch issues a PATCH to the specified URL.

Caller should close resp.Body when done reading from it.

func Post

func Post(url string, a *Args) (resp *Response, err error)

Post issues a POST to the specified URL.

Caller should close resp.Body when done reading from it.

func Put

func Put(url string, a *Args) (resp *Response, err error)

Put issues a PUT to the specified URL.

Caller should close resp.Body when done reading from it.

func (*Response) Content

func (resp *Response) Content() (b []byte, err error)

Get Response Body as []byte

func (*Response) Json

func (resp *Response) Json() (*simplejson.Json, error)

Get Response Body as simplejson.Json

func (*Response) OK

func (resp *Response) OK() bool

Does Response StatusCode < 400 ?

func (*Response) Ok

func (resp *Response) Ok() bool

Does Response StatusCode < 400 ?

func (*Response) Reason

func (resp *Response) Reason() string

Get Response Status

func (*Response) Text

func (resp *Response) Text() (string, error)

Get Response Body as string

func (*Response) URL

func (resp *Response) URL() (*url.URL, error)

Jump to

Keyboard shortcuts

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