requests

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

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

Go to latest
Published: Jan 20, 2016 License: Apache-2.0 Imports: 13 Imported by: 1

README

#Requests Build Status

Simple HTTP library, do the same work as Requests, but for Go.

OVERVIEW

To GET or POST or PUT or DELETE...

import "github.com/ideazxy/requests"

getResp, err := requests.Get("http://httpbin.org/get").Send()

postResp, err := requests.Post("http://httpbin.org/post", "string or []byte or io.Reader or io.ReadCloser", "text/plain").Send()

putResp, err := requests.Put("http://httpbin.org/put", []byte("the same as post api"), "application/json").Send()

delResp, err := requests.Delete("http://httpbin.org/delete").Send()

Do more settings in pipline style:

resp, err := requests.Get("http://httpbin.org/get").SetHeader("User-Agent", "Chrome").AddParam("key", "value").AllowRedirects(false).Send()

Fetch data from reponse:

// Get string:
s, err := resp.Text()

// Get []byte
b, err := resp.Content()

// Get Json struct
var j struct{Msg `json:"message"`}
err := resp.Json(&j)

// Get Xml struct
var x struct{Msg `xml:"message"`}
err := resp.Xml(&x)

INSTALLATION

To install:

go get github.com/ideazxy/requests

LICENSE

requests is licensed under the Apache Licence, Version 2.0
http://www.apache.org/licenses/LICENSE-2.0.html.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultUserAgent = "Go Requests"

Functions

This section is empty.

Types

type Client

type Client struct {
	http.Client

	RedirectMax int
	// contains filtered or unexported fields
}

func NewClient

func NewClient() *Client

func (*Client) Do

func (c *Client) Do(req *http.Request) (*HttpResponse, error)

func (*Client) ForbidRedirects

func (c *Client) ForbidRedirects()

func (*Client) SetTimeout

func (c *Client) SetTimeout(timeout time.Duration)

type HttpRequest

type HttpRequest struct {
	Client *Client

	Req    *http.Request
	Params url.Values
	// contains filtered or unexported fields
}

func Delete

func Delete(url string) *HttpRequest

func Get

func Get(url string) *HttpRequest
func Head(url string) *HttpRequest

func NewRequest

func NewRequest(method, rawurl string) *HttpRequest

func Options

func Options(url string) *HttpRequest

func Patch

func Patch(url string, data interface{}) *HttpRequest

func Post

func Post(url string, data interface{}, bodyType string) *HttpRequest

func PostForm

func PostForm(u string, data url.Values) *HttpRequest

func Put

func Put(url string, data interface{}, bodyType string) *HttpRequest

func Request

func Request(method, url string) *HttpRequest

func (*HttpRequest) AddCookie

func (r *HttpRequest) AddCookie(c *http.Cookie) *HttpRequest

AddCookie does not attach more than one Cookie header field. That means all cookies, if any, are written into the same line, separated by semicolon.

func (*HttpRequest) AddHeader

func (r *HttpRequest) AddHeader(key, value string) *HttpRequest

Add adds the key, value pair to the header. It appends to any existing values associated with key.

func (*HttpRequest) AddParam

func (r *HttpRequest) AddParam(key, value string) *HttpRequest

func (*HttpRequest) AllowRedirects

func (r *HttpRequest) AllowRedirects(allow bool) *HttpRequest

func (*HttpRequest) Cookie

func (r *HttpRequest) Cookie(name string) (*http.Cookie, error)

Cookie returns the named cookie provided in the request or ErrNoCookie if not found.

func (*HttpRequest) Cookies

func (r *HttpRequest) Cookies() []*http.Cookie

Cookies parses and returns the HTTP cookies sent with the request.

func (*HttpRequest) DelHeader

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

Del deletes the values associated with key.

func (*HttpRequest) DelParam

func (r *HttpRequest) DelParam(key string) *HttpRequest

func (*HttpRequest) Header

func (r *HttpRequest) Header(key string) string

Get gets the first value associated with the given key. If there are no values associated with the key, Get returns "". To access multiple values of a key, access the map directly with CanonicalHeaderKey.

func (*HttpRequest) Param

func (r *HttpRequest) Param(key string) string

Get gets the first value associated with the given key. If there are no values associated with the key, Get returns the empty string. To access multiple values, use the map directly.

func (*HttpRequest) Path

func (r *HttpRequest) Path() string

func (*HttpRequest) Prepare

func (r *HttpRequest) Prepare() error

func (*HttpRequest) Query

func (r *HttpRequest) Query() url.Values

func (*HttpRequest) Send

func (r *HttpRequest) Send() (*HttpResponse, error)

func (*HttpRequest) SetBody

func (r *HttpRequest) SetBody(data interface{}, bodyType string) *HttpRequest

func (*HttpRequest) SetHeader

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

Set sets the header entries associated with key to the single element value. It replaces any existing values associated with key.

func (*HttpRequest) SetParam

func (r *HttpRequest) SetParam(key, value string) *HttpRequest

func (*HttpRequest) SetRedirectMax

func (r *HttpRequest) SetRedirectMax(count int) *HttpRequest

func (*HttpRequest) Timeout

func (r *HttpRequest) Timeout(timeout time.Duration) *HttpRequest

func (*HttpRequest) Url

func (r *HttpRequest) Url() string

type HttpResponse

type HttpResponse struct {
	Resp              *http.Response
	StatusCode        int
	RedirectForbidden bool
	// contains filtered or unexported fields
}

func NewResponse

func NewResponse(resp *http.Response) *HttpResponse

func (*HttpResponse) Content

func (r *HttpResponse) Content() ([]byte, error)

func (*HttpResponse) Cookie

func (r *HttpResponse) Cookie(name string) *http.Cookie

func (*HttpResponse) Cookies

func (r *HttpResponse) Cookies() []*http.Cookie

Cookies parses and returns the cookies set in the Set-Cookie headers.

func (*HttpResponse) Json

func (r *HttpResponse) Json(v interface{}) error

func (*HttpResponse) Location

func (r *HttpResponse) Location() (*url.URL, error)

Location returns the URL of the response's "Location" header, if present. Relative redirects are resolved relative to the Response's Request. ErrNoLocation is returned if no Location header is present.

func (*HttpResponse) Text

func (r *HttpResponse) Text() (string, error)

func (*HttpResponse) Xml

func (r *HttpResponse) Xml(v interface{}) error

type RedirectForbiddenError

type RedirectForbiddenError struct {
}

func NewRedirectForbiddenError

func NewRedirectForbiddenError() *RedirectForbiddenError

func (*RedirectForbiddenError) Error

func (e *RedirectForbiddenError) Error() string

type Session

type Session struct {
	Client    *Client
	UserAgent string
	// contains filtered or unexported fields
}

func NewSession

func NewSession() *Session

func (*Session) Cookie

func (s *Session) Cookie(key string, url *url.URL) (cookie *http.Cookie)

func (*Session) Cookies

func (s *Session) Cookies(url *url.URL) (cookies []*http.Cookie)

func (*Session) Delete

func (s *Session) Delete(url string) *HttpRequest

func (*Session) Get

func (s *Session) Get(url string) *HttpRequest

func (*Session) Head

func (s *Session) Head(url string) *HttpRequest

func (*Session) Options

func (s *Session) Options(url string) *HttpRequest

func (*Session) Patch

func (s *Session) Patch(url string, data interface{}) *HttpRequest

func (*Session) Post

func (s *Session) Post(url string, data interface{}, bodyType string) *HttpRequest

func (*Session) PostForm

func (s *Session) PostForm(u string, data url.Values) *HttpRequest

func (*Session) Put

func (s *Session) Put(url string, data interface{}, bodyType string) *HttpRequest

func (*Session) Request

func (s *Session) Request(method, url string) *HttpRequest

Jump to

Keyboard shortcuts

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