gorequest

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2014 License: MIT Imports: 7 Imported by: 0

README

GoRequest

GoRequest -- Simplified HTTP client ( inspired by famous SuperAgent lib in Node.js )

Installation

$ go get github.com/parnurzeal/gorequest

Documentation

See Go Doc for usage and details.

Status

Drone Build Status Travis Build Status

Why should you use GoRequest?

GoRequest makes thing much more simple for you, making http client more awesome and fun like SuperAgent + golang style usage.

This is what you normally do for a simple GET without GoRequest:

resp, err := http.Get("http://example.com/")

With GoRequest:

request := gorequest.New()
resp, body, err := request.Get("http://example.com/").End()

Or below if you don't want to reuse it for other requests.

resp, body, err := gorequest.New().Get("http://example.com/").End()

How about getting control over HTTP client headers, redirect policy, and etc. Things is getting more complicated in golang. You need to create a Client, setting header in different comamnd, ... to do just only one GET

client := &http.Client{
  CheckRedirect: redirectPolicyFunc,
}

req, err := http.NewRequest("GET", "http://example.com", nil)

req.Header.Add("If-None-Match", `W/"wyzzy"`)
resp, err := client.Do(req)

Why making things ugly while you can just do as follows:

request := gorequest.New()
resp, body, err := request.Get("http://example.com").
  RedirectPolicy(redirectPolicyFunc).
  Set("If-None-Match", `W/"wyzzy"`).
  End()

For a JSON POST with standard libraries, you might need to marshal map data structure to json format, setting header to 'application/json' (and other headers if you need to) and declare http.Client. So, you code become longer and hard to maintain:

m := map[string]interface{}{
  "name": "backy",
  "species": "dog",
}
mJson, _ := json.Marshal(m)
contentReader := bytes.NewReader(mJson)
req, _ := http.NewRequest("POST", "http://example.com", contentReader)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Notes","GoRequest is coming!")
client := &http.Client{}
resp, _ := client.Do(req)

Compared to our GoRequest version, JSON is for sure a default. So, it turns out to be just one simple line!:

request := gorequest.New()
resp, body, err := request.Post("http://example.com").
  Set("Notes","gorequst is coming!").
  Send(`{"name":"backy", "species":"dog"}`).
  End()

Note: This is a work in progress and not totally support all specifications. Right now, you can do simple get and post with easy to specify header like in examples which is enough in many cases.

License

GoRequest is MIT License.

Documentation

Overview

Package gorequest inspired by Nodejs SuperAgent provides easy-way to write http client

Index

Constants

This section is empty.

Variables

View Source
var Types = map[string]string{
	"html":       "text/html",
	"json":       "application/json",
	"xml":        "application/xml",
	"urlencoded": "application/x-www-form-urlencoded",
	"form":       "application/x-www-form-urlencoded",
	"form-data":  "application/x-www-form-urlencoded",
}

Functions

This section is empty.

Types

type Request

type Request *http.Request

type Response

type Response *http.Response

type SuperAgent

type SuperAgent struct {
	Url        string
	Method     string
	Header     map[string]string
	TargetType string
	ForceType  string
	Data       map[string]interface{}
	FormData   url.Values
	QueryData  url.Values
	Client     *http.Client
}

A SuperAgent is a object storing all request data for client.

func New

func New() *SuperAgent

Used to create a new SuperAgent object.

func (*SuperAgent) ClearSuperAgent

func (s *SuperAgent) ClearSuperAgent()

Clear SuperAgent data for another new request.

func (*SuperAgent) End

func (s *SuperAgent) End(callback ...func(response Response, body string)) (Response, string, error)

func (*SuperAgent) Get

func (s *SuperAgent) Get(targetUrl string) *SuperAgent

func (*SuperAgent) Post

func (s *SuperAgent) Post(targetUrl string) *SuperAgent

func (*SuperAgent) Query

func (s *SuperAgent) Query(content string) *SuperAgent

Query method accepts ether json string or strings which will form a query-string in url of GET method or body of POST method. For example, making "/search?query=bicycle&size=50x50&weight=20kg" using GET method:

gorequest.New().
  Get('/search').
  Query(`{ query: 'bicycle' }`).
  Query(`{ size: '50x50' }`).
  Query(`{ weight: '20kg' }`).
  End()

Or you can put multiple json values:

gorequest.New().
  Get('/search').
  Query(`{ query: 'bicycle', size: '50x50', weight: '20kg' }`).
  End()

Strings are also acceptable:

gorequest.New().
  Get('/search').
  Query('query=bicycle&size=50x50').
  Query('weight=20kg').
  End()

Or even Mixed! :)

gorequest.New().
  Get('/search').
  Query('query=bicycle').
  Query(`{ size: '50x50', weight:'20kg' }`).
  End()

TODO: check error

func (*SuperAgent) RedirectPolicy

func (s *SuperAgent) RedirectPolicy(policy func(req Request, via []Request) error) *SuperAgent

func (*SuperAgent) Send

func (s *SuperAgent) Send(content string) *SuperAgent

func (*SuperAgent) Set

func (s *SuperAgent) Set(param string, value string) *SuperAgent

func (*SuperAgent) Type

func (s *SuperAgent) Type(typeStr string) *SuperAgent

Jump to

Keyboard shortcuts

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