gorequester

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2020 License: Apache-2.0 Imports: 12 Imported by: 0

README

gorequester

Golang chain style http request lib

Install

go get github.com/cheetah-fun-gs/gorequester

How

package main

import (
	"fmt"
	"net/http"
	"time"

	requester "github.com/cheetah-fun-gs/gorequester"
)

func main() {
	// Get
	fmt.Println(requester.New("GET", "http://httpbin.org/get?abc=123").ReadString())
	fmt.Println(requester.New("GET", "http://httpbin.org/get?abc=123").AddRawQuery("def=456").ReadString())

	m := map[string]interface{}{
		"qewr": "424",
		"uio":  1231,
	}
	fmt.Println(requester.Get("http://httpbin.org/get?abc=123", m).ReadString())
	fmt.Println(requester.Get("http://httpbin.org/get?abc=123").AddRawQuery(m).ReadString())

	s := struct {
		FDI string `json:"fdi,omitempty"`
		IFU int    `json:"ifu,omitempty"`
	}{
		FDI: "9f0d",
		IFU: 413,
	}
	fmt.Println(requester.Get("http://httpbin.org/get?abc=123", s).ReadString())
	fmt.Println(requester.Get("http://httpbin.org/get?abc=123").AddRawQuery(s).ReadString())

	fmt.Println(requester.Get("http://httpbin.org/get?abc=123", m, s).ReadString())

	// Post Raw
	fmt.Println(requester.New("POST", "http://httpbin.org/post?abc=123").SetRawData(m).ReadString())
	fmt.Println(requester.Post("http://httpbin.org/post?abc=123").SetRawData(s).ReadString())
	fmt.Println(requester.PostData("http://httpbin.org/post?abc=123", "application/octet-stream", "abf").ReadString())
	fmt.Println(requester.PostJSON("http://httpbin.org/post?abc=123", s).ReadString())

	// Post Form
	m2 := map[string]interface{}{
		"qewr": "424",
		"uio":  []string{"dfa", "sf"},
		"fdf":  424,
		"gege": []int{3213, 4213},
	}
	fmt.Println(requester.PostForm("http://httpbin.org/post?abc=123", m2).ReadString())
	fmt.Println(requester.Post("http://httpbin.org/post?abc=123").SetFormFields(m2).ReadString())

	s2 := struct {
		FDI string   `json:"fdi,omitempty"`
		IFU int      `json:"ifu,omitempty"`
		IFI []int    `json:"ifi,omitempty"`
		GUE []string `json:"gue,omitempty"`
	}{
		FDI: "9f0d",
		IFU: 413,
		IFI: []int{12, 421},
		GUE: []string{"fs", "dsf"},
	}
	fmt.Println(requester.Post("http://httpbin.org/post?abc=123").SetFormFields(s2).ReadString())

	// Post File
	// f := &requester.FormFile{
	// 	FieldName: "file",
	// 	FileName:  "abc.txt",
	// 	FilePath:  "xxx",
	// }
	// fmt.Println(requester.Post("http://httpbin.org/post?abc=123").SetFormFields(s2).AddFormFile(f).ReadString())

	// JSON Resp
	r := map[string]interface{}{}
	if err := requester.Post("http://httpbin.org/post?abc=123").SetFormFields(s2).ReadJSON(&r); err != nil {
		fmt.Println(err)
	} else {
		fmt.Println(r)
	}

	// Custom Client
	c := &http.Client{
		Timeout: time.Second * 5,
	}
	fmt.Println(requester.NewWithClient("GET", "http://httpbin.org/get?abc=123", c).ReadString())
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FormFile

type FormFile struct {
	FieldName string `json:"field_name,omitempty"`
	FilePath  string `json:"file_path,omitempty"`
	FileName  string `json:"file_name,omitempty"`
}

FormFile 文件表单数据

type Requester

type Requester struct {
	Error error
	// contains filtered or unexported fields
}

Requester http.Request

func Get

func Get(toURL string, v ...interface{}) *Requester

Get 创建一个 GET Requester

func New

func New(method, toURL string) *Requester

New 创建一个基础 Requester

func NewWithClient

func NewWithClient(method, toURL string, client *http.Client) *Requester

NewWithClient 创建一个基础 Requester

func Post

func Post(toURL string) *Requester

Post 创建一个 POST Requester

func PostData

func PostData(toURL, contentType string, v interface{}) *Requester

PostData PostData

func PostForm added in v1.0.0

func PostForm(toURL string, v interface{}) *Requester

PostForm PostForm

func PostJSON

func PostJSON(toURL string, v interface{}) *Requester

PostJSON PostJSON

func (*Requester) AddFormField

func (req *Requester) AddFormField(key string, val interface{}) *Requester

AddFormField 追加 formField

func (*Requester) AddFormFile

func (req *Requester) AddFormFile(files ...*FormFile) *Requester

AddFormFile 追加 formFile

func (*Requester) AddHeader

func (req *Requester) AddHeader(key, val string) *Requester

AddHeader 添加请求头

func (*Requester) AddRawQuery

func (req *Requester) AddRawQuery(v ...interface{}) *Requester

AddRawQuery 追加 RawQuery

func (*Requester) Client

func (req *Requester) Client() *http.Client

Client Client

func (*Requester) Do

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

Do 获取响应

func (*Requester) FormFields added in v1.0.1

func (req *Requester) FormFields() url.Values

FormFields FormFields

func (*Requester) RawData added in v1.0.1

func (req *Requester) RawData() []byte

RawData RawData

func (*Requester) ReadData

func (req *Requester) ReadData() ([]byte, error)

ReadData 获取响应二进制响应

func (*Requester) ReadJSON

func (req *Requester) ReadJSON(v interface{}) error

ReadJSON 获取响应JSON响应

func (*Requester) ReadString

func (req *Requester) ReadString() (string, error)

ReadString 获取响应字符串响应

func (*Requester) Request

func (req *Requester) Request() *http.Request

Request Request

func (*Requester) SetFormField

func (req *Requester) SetFormField(key string, val interface{}) *Requester

SetFormField 设置 formField

func (*Requester) SetFormFields

func (req *Requester) SetFormFields(v interface{}) *Requester

SetFormFields 设置 formField v type in ( string, struct, map[string]string, map[string][]string, map[string]int, map[string][]int )

func (*Requester) SetHeader

func (req *Requester) SetHeader(key, val string) *Requester

SetHeader 设置请求头

func (*Requester) SetRawData

func (req *Requester) SetRawData(v interface{}) *Requester

SetRawData 设置 RawData

func (*Requester) SetRawQuery

func (req *Requester) SetRawQuery(v ...interface{}) *Requester

SetRawQuery 重设 RawQuery

func (*Requester) URL

func (req *Requester) URL() *url.URL

URL URL

Jump to

Keyboard shortcuts

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