request

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2015 License: MIT Imports: 16 Imported by: 50

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

Examples

Constants

View Source
const Version = "0.4.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 added in v0.2.0

type BasicAuth struct {
	Username string
	Password string
}

type FileField

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

type Request added in v0.3.0

type Request struct {
	*Args
}

func NewRequest added in v0.3.0

func NewRequest(c *http.Client) *Request

func (*Request) Delete added in v0.3.0

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

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

func (*Request) Get added in v0.3.0

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

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

func (*Request) Head added in v0.3.0

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

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

func (*Request) Options added in v0.3.0

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

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

func (*Request) Patch added in v0.3.0

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

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

func (*Request) Post added in v0.3.0

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

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

func (*Request) Put added in v0.3.0

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

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

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.

Example
package main

import (
	"fmt"
	"net/http"

	"github.com/mozillazg/request"
)

func main() {
	c := new(http.Client)
	req := request.NewRequest(c)
	url := "http://httpbin.org/get"
	resp, _ := req.Get(url)
	d, _ := resp.Json()
	defer resp.Body.Close()
	fmt.Println(resp.Ok())
	fmt.Println(d.Get("url").MustString())
}
Output:

true
http://httpbin.org/get
Example (Cookies)
package main

import (
	"net/http"

	"github.com/mozillazg/request"
)

func main() {
	c := new(http.Client)
	req := request.NewRequest(c)
	req.Cookies = map[string]string{
		"name": "value",
		"foo":  "bar",
	}
	url := "http://httpbin.org/cookies"
	resp, _ := req.Get(url)
	defer resp.Body.Close()
}
Output:

Example (CustomHeaders)
package main

import (
	"fmt"
	"net/http"

	"github.com/mozillazg/request"
)

func main() {
	c := new(http.Client)
	req := request.NewRequest(c)
	req.Headers = map[string]string{
		"X-Abc":      "abc",
		"User-Agent": "go-request-test",
	}
	url := "http://httpbin.org/get"
	resp, _ := req.Get(url)
	d, _ := resp.Json()
	defer resp.Body.Close()
	fmt.Println(d.Get("headers").Get("User-Agent").MustString())
	fmt.Println(d.Get("headers").Get("X-Abc").MustString())
}
Output:

go-request-test
abc
Example (Params)
package main

import (
	"fmt"
	"net/http"

	"github.com/mozillazg/request"
)

func main() {
	c := new(http.Client)
	req := request.NewRequest(c)
	req.Params = map[string]string{
		"a": "1",
		"b": "2",
	}
	url := "http://httpbin.org/get"
	resp, _ := req.Get(url)
	d, _ := resp.Json()
	defer resp.Body.Close()
	fmt.Println(d.Get("url").MustString())
}
Output:

http://httpbin.org/get?a=1&b=2
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.

Example
package main

import (
	"net/http"

	"github.com/mozillazg/request"
)

func main() {
	c := new(http.Client)
	req := request.NewRequest(c)
	req.Data = map[string]string{
		"a": "1",
		"b": "2",
	}
	url := "http://httpbin.org/post"
	resp, _ := req.Post(url)
	defer resp.Body.Close()
}
Output:

Example (Files)
package main

import (
	"net/http"
	"os"

	"github.com/mozillazg/request"
)

func main() {
	c := new(http.Client)
	req := request.NewRequest(c)
	f, _ := os.Open("test.txt")
	defer f.Close()
	req.Files = []request.FileField{
		request.FileField{"abc", "abc.txt", f},
	}
	url := "http://httpbin.org/post"
	resp, _ := req.Post(url)
	defer resp.Body.Close()
}
Output:

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 added in v0.2.0

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

Directories

Path Synopsis
_example

Jump to

Keyboard shortcuts

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