requests4go

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2021 License: Apache-2.0 Imports: 16 Imported by: 1

README

request4go

test Go Reference

Go HTTP Requests. ✨🎉✨ Send requests quickly and humanely.

Quick Start

Get module.

go get -u github.com/Kaiser925/requests4go
Make a request
package main

import (
	"log"

	"github.com/Kaiser925/requests4go"
)

func main() {
	r, err := requests4go.Get("http://httpbin.org/get")
	if err != nil {
		log.Fatal(err.Error())
	}

	txt, _ := r.Text()
	log.Println(txt)
}

You can also send a POST request.

package main

import (
	"log"

	"github.com/Kaiser925/requests4go"
)

func main() {
	// JSON will set body be json data.
	data := requests4go.JSON(requests4go.M{"key": "value"})
	r, err := requests4go.Post("http://httpbin.org/post", data)
	
	// handle r and err
}
Passing Parameters In URLS
params := requests4go.Params(requests4go.M{"key1": "value1", "key2": "value2"})
r, err := requests4go.Get("http://httpbin.org/get", params)
Custom Headers
	headers := requests4go.Headers(requests4go.M{"key1": "value1", "key2": "value2"})

	r, err := requests4go.Get("http://httpbin.org/get", headers)
Response Content

We can read the content of the server's response.

resp, _ := requests4go.Get("https://httpbin.org/get", nil)
txt, _ := resp.Text()
log.Println(txt)
JSON Response Content

There are two methods to handle JSON response content.

  1. We can deal with SimpleJSON witch use go-simplejson parse json data.
resp, _ := requests4go.Get("https://httpbin.org/get")
j, _ := resp.SimpleJSON()
url, _ := j.Get("url").String()
log.Println(url)
  1. We can unmarshal the struct by using JSON.
foo := &Foo{}
resp, _ := requests4go.Get("https://example.com")
j, _ := resp.JSON(foo)
log.Println(j.bar)

License

Apache License, Version 2.0. See LICENSE for the full license text

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrRedirectLimitExceeded will be returned when redirect times over limit.
	ErrRedirectLimitExceeded = errors.New("requests4go: Request exceeded redirect count limit")
)

Functions

func NewRequest added in v0.1.1

func NewRequest(method, url string, opts ...RequestOption) (*http.Request, error)

NewRequest wrappers the NewRequestWithContext

func NewRequestWithContext added in v0.1.1

func NewRequestWithContext(ctx context.Context, method, url string, opts ...RequestOption) (*http.Request, error)

NewRequestWithContext builds a new *http.Request with Context and RequestOption

Note: If multiple option will modify the request body, only the last one will take effect. The order of options all will effect final request status.

Types

type M added in v0.4.0

type M = map[string]string

M is a shortcut for map[string]string

type RequestOption added in v0.1.1

type RequestOption func(req *http.Request) error

A RequestOption is represent a option of request. You can use it to custom request. You can also define your own RequestOption.

func Auth added in v0.1.1

func Auth(name string, password string) RequestOption

Auth sets basic auth for the request.

func Body added in v0.3.0

func Body(body io.Reader) RequestOption

Body sets request body.

func Cookies added in v0.1.1

func Cookies(c map[string]string) RequestOption

Cookies add the cookie to http.Request.

func Data added in v0.1.1

func Data(form map[string]string) RequestOption

Data sends form-encoded data.

func FileContent added in v0.3.0

func FileContent(filename string) RequestOption

FileContent loads file content, and set it be request body.

func Headers added in v0.1.1

func Headers(headers map[string]string) RequestOption

Headers sets the header for the request. It replaces any existing values. The key is case insensitive.

func JSON added in v0.1.1

func JSON(v interface{}) RequestOption

JSON encodes the value to json data and set content-type be application/json.

func MultipartForm added in v0.4.0

func MultipartForm(form map[string]io.Reader) RequestOption

MultipartForm sets a multipart/form-data request body.

func Params added in v0.1.1

func Params(params map[string]string) RequestOption

Params sets url query parameters for the request. It replaces any existing values.

type Response

type Response struct {
	*http.Response
}

Response is a wrapper of the http.Response. It opens up new methods for http.Response.

func Delete

func Delete(url string, opts ...RequestOption) (*Response, error)

Delete sends "DELETE" request.

func Do added in v0.1.1

func Do(req *http.Request) (*Response, error)

Do sends request and return the response.

func Get

func Get(url string, opts ...RequestOption) (*Response, error)

Get sends "GET" request.

func Head(url string, opts ...RequestOption) (*Response, error)

Head sends "HEAD" request.

func NewResponse

func NewResponse(resp *http.Response) *Response

NewResponse returns new Response

func Options

func Options(url string, opts ...RequestOption) (*Response, error)

Options sends "OPTIONS" request.

func Patch

func Patch(url string, opts ...RequestOption) (*Response, error)

Patch sends "PATCH" request.

func Post

func Post(url string, opts ...RequestOption) (*Response, error)

Post sends "POST" request.

func Put

func Put(url string, opts ...RequestOption) (*Response, error)

Put sends "Put" request.

func (*Response) Close

func (r *Response) Close() error

Close is to support io.ReadCloser.

func (*Response) Content

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

Content reads body of response and returns content of response in bytes.

func (*Response) JSON added in v0.1.1

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

JSON reads body of response and unmarshal the response content to v.

func (*Response) Ok

func (r *Response) Ok() bool

Ok returns true if the status code is less than 400.

func (*Response) Read

func (r *Response) Read(p []byte) (n int, err error)

Read is to support io.ReadCloser.

func (*Response) SaveContent

func (r *Response) SaveContent(filename string) error

SaveContent reads body of response and saves response body to file.

func (*Response) SimpleJSON added in v0.1.1

func (r *Response) SimpleJSON() (*simplejson.Json, error)

SimpleJSON reads body of response and returns simplejson.Json. See the usage of simplejson on https://godoc.org/github.com/bitly/go-simplejson.

func (*Response) Text

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

Text reads body of response and returns content of response in string.

func (*Response) XML added in v0.4.0

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

XML unmarshal the response content as XML.

type Session

type Session struct {
	Client *http.Client
}

Session allows user use cookies between HTTP requests.

func NewSession

func NewSession() *Session

NewSession returns a session struct.

func (*Session) Delete

func (s *Session) Delete(url string, opts ...RequestOption) (*Response, error)

Delete sends a DELETE request, returns Response struct.

func (*Session) Get

func (s *Session) Get(url string, opts ...RequestOption) (*Response, error)

Get sends a GET request, returns Response struct.

func (*Session) Head

func (s *Session) Head(url string, opts ...RequestOption) (*Response, error)

Head sends a HEAD request, returns Response struct.

func (*Session) Options

func (s *Session) Options(url string, opts ...RequestOption) (*Response, error)

Options sends a OPTIONS request, returns Response struct.

func (*Session) Patch

func (s *Session) Patch(url string, opts ...RequestOption) (*Response, error)

Patch sends a PATCH request, returns Response struct.

func (*Session) Post

func (s *Session) Post(url string, opts ...RequestOption) (*Response, error)

Post sends a POST request, returns Response struct.

func (*Session) Put

func (s *Session) Put(url string, opts ...RequestOption) (*Response, error)

Put sends a PUT request, returns Response struct.

Jump to

Keyboard shortcuts

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