requests

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2021 License: MIT Imports: 9 Imported by: 1

README

requests

An elegant and simple HTTP library for golang.

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

This section is empty.

Types

type Option

type Option func(*Options)

Option is the functional option type.

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.

type Options

type Options struct {
	Headers map[string]string
	Params  map[string]string
	Form    map[string]string
	JSON    interface{}
}

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(url string, setters ...Option) (*Response, error)

Delete issues a http DELETE request.

func Get

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

Get issues a http GET request.

func Post

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

Post issues a http POST request.

func Put

func Put(url 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) 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