request

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2015 License: MIT Imports: 12 Imported by: 0

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 := &http.Client{}
a := request.NewArgs(c)
resp, err := request.Get("http://httpbin.org/get", a)
j, err := resp.Json()
defer resp.Body.Close()  // Don't forget close the response body

POST:

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

Cookies:

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

Headers:

a.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 := request.Get("http://httpbin.org/get", a)

Files:

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

Json:

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

Documentation

Overview

Go HTTP Requests for Humans™.

HTTP Request is so easy:

GET Request

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

POST Request

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

Custom Cookies

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

Custom Headers

a.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 := request.Get("http://httpbin.org/get", a)

Upload Files

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

Json Body

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

Index

Examples

Constants

View Source
const Version = "0.1.0"

Variables

This section is empty.

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

func NewArgs

func NewArgs(c *http.Client) *Args

type FileField

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

type Response

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

func Delete

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

func Get

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

import (
	"fmt"
	"net/http"

	"github.com/mozillazg/request"
)

func main() {
	c := &http.Client{}
	a := request.NewArgs(c)
	url := "http://httpbin.org/get"
	resp, _ := request.Get(url, a)
	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 := &http.Client{}
	a := request.NewArgs(c)
	a.Cookies = map[string]string{
		"name": "value",
		"foo":  "bar",
	}
	url := "http://httpbin.org/cookies"
	resp, _ := request.Get(url, a)
	defer resp.Body.Close()
}
Output:

Example (CustomHeaders)
package main

import (
	"fmt"
	"net/http"

	"github.com/mozillazg/request"
)

func main() {
	c := &http.Client{}
	a := request.NewArgs(c)
	a.Headers = map[string]string{
		"X-Abc":      "abc",
		"User-Agent": "go-request-test",
	}
	url := "http://httpbin.org/get"
	resp, _ := request.Get(url, a)
	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 := &http.Client{}
	a := request.NewArgs(c)
	a.Params = map[string]string{
		"a": "1",
		"b": "2",
	}
	url := "http://httpbin.org/get"
	resp, _ := request.Get(url, a)
	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)

func Options

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

func Patch

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

func Post

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

import (
	"net/http"

	"github.com/mozillazg/request"
)

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

Example (Files)
package main

import (
	"net/http"
	"os"

	"github.com/mozillazg/request"
)

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

func Put

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

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

Jump to

Keyboard shortcuts

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