requests

package module
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2021 License: MIT Imports: 11 Imported by: 1

README

requests

An elegant and simple HTTP library for golang.

Click to read documentation.

Documentation

Overview

Package requests is an elegant and simple HTTP library for golang, built for human beings.

This package mimics the implementation of the classic Python package Requests(https://requests.readthedocs.io/)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetEnvTimeout added in v0.0.7

func SetEnvTimeout(timeout int64)

Types

type Auth added in v0.0.5

type Auth struct {
	// contains filtered or unexported fields
}

type AuthType added in v0.0.5

type AuthType int
const (
	HTTPNoAuth AuthType = iota
	HTTPBasicAuth
	HTTPProxyAuth  // TODO
	HTTPDigestAuth // TODO
)

type Env added in v0.0.7

type Env struct {
	Timeout int64
}

type Option

type Option func(*Options)

Option is the functional option type.

func BasicAuth added in v0.0.5

func BasicAuth(username, password string) Option

BasicAuth is the option to implement HTTP Basic Auth.

func Body added in v0.0.7

func Body(body io.Reader) Option

Body set io.Reader to hold request body.

func Data added in v0.0.4

func Data(data interface{}) Option

Data set raw string into the request body.

func Form

func Form(form map[string]string) Option

Form encode the given KV into the request body. It also sets the Content-Type as "application/x-www-form-urlencoded".

func FormPairs

func FormPairs(kv ...string) Option

FormPairs returns an Form formed by the mapping of key, value ... Pairs panics if len(kv) is odd.

func HeaderPairs

func HeaderPairs(kv ...string) Option

HeaderPairs returns an Headers formed by the mapping of key, value ... Pairs panics if len(kv) is odd.

func Headers

func Headers(headers map[string]string) Option

Headers set the HTTP header KVs.

func JSON

func JSON(obj interface{}) Option

JSON serializes the given struct as JSON into the request body. It also sets the Content-Type as "application/json".

func ParamPairs

func ParamPairs(kv ...string) Option

ParamPairs returns an Params formed by the mapping of key, value ... Pairs panics if len(kv) is odd.

func Params

func Params(params map[string]string) Option

Params encode the given KV into the URL querystring.

func Timeout added in v0.0.7

func Timeout(timeout int64) Option

Timeout specifies a time limit for requests made by this Client. The timeout includes connection time, any redirects, and reading the response body. The timer remains running after Get, Head, Post, or Do return and will interrupt reading of the Response.Body.

A Timeout of zero means no timeout. Default is 60s.

type Options

type Options struct {
	Headers map[string]string
	Params  map[string]string
	// body
	Body io.Reader
	// different body types
	Data interface{}
	Form map[string]string
	JSON interface{}

	// auth
	Auth Auth
	// timeout seconds
	Timeout int64
}

Options follow the design of Functional Options(https://github.com/tmrts/go-patterns/blob/master/idiom/functional-options.md)

type Response

type Response struct {
	// contains filtered or unexported fields
}

Response is a wrapper of http response.

func Delete

func Delete(rawurl string, setters ...Option) (*Response, error)

Delete issues a http DELETE request.

func Get

func Get(rawurl string, setters ...Option) (*Response, error)

Get issues a http GET request.

func Post

func Post(rawurl string, setters ...Option) (*Response, error)

Post issues a http POST request.

func Put

func Put(rawurl string, setters ...Option) (*Response, error)

Put issues a http PUT request.

func (*Response) Close

func (r *Response) Close() error

Close closes the http response body read stream.

func (*Response) Cookies added in v0.0.9

func (r *Response) Cookies() map[string]*http.Cookie

func (*Response) Headers added in v0.0.9

func (r *Response) Headers() http.Header

func (*Response) JSON

func (r *Response) JSON(v interface{}) error

JSON decode the http response body as JSON format.

func (*Response) Method

func (r *Response) Method() string

Method get the http request method.

func (*Response) Raw

func (r *Response) Raw() io.ReadCloser

Raw get the raw socket response from the server.

func (*Response) StatusCode

func (r *Response) StatusCode() int

StatusCode get status code of http response.

func (*Response) Text

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

Text return the http response body as string.

func (*Response) URL

func (r *Response) URL() string

URL get the http request url string.

Jump to

Keyboard shortcuts

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