fetch

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2020 License: MIT Imports: 7 Imported by: 0

README

go-fetch

a http client like node-fetch

usage

go get -u github.com/zzfup/go-fetch

API

Fetch(url[, options])

  • url: string. should be an absolute url, such as http://example.com/.

  • options: map[string]string. the default values are here:

    Options{
    	Method: "GET",
    	Header: map[string]string{
    		"Accept-Encoding": "gzip,deflate",
    		"Accept":          "*/*",
    	},
     	Body:    nil,
    	Timeout: 20 * time.Millisecond,
    }
    

NewDefaultOptions()

return a defautl options.

you can also create an Options, like this:

options := fetch.Options{
    Method: "POST",
    Header: headers,  // your custom header, its type is map[string]string
    Body: payload, // your request body, its type is []byte
}

ToString()

convert the respone body to string

BindJSON(i interface{})

convert the response body to a struct or a map

example

options := fetch.Options{}
// option = fetch.NewDefaulOptions()
resp, err := fetch.Fetch("https://www.baidu.com", fetch.Options{})
fmt.Println(err)
fmt.Println(resp.ToString())

GET

import "github.com/zzfup/go-fetch"
import "fmt"

// header can be just like this
var headers = map[string]string{
	"Accept":       "application/json, text/plain, */*",
	"Content-Type": "application/json",
}

func main(){
    	url := "https://www.example.com"
    	options := fetch.Options{
		Method: "GET",
        	Header: headers,
        	Timeout: 2 * time.Second,
    	}

    	resp, err := fetch.Fetch(url, options)
	if err != nil {
		return  err
	}

    	fmt.Println(resp.StatusCode)
    	fmt.Println(resp.ToString)
    	var j struct{
        	Test string `json:"test"`
    	}
    	err := resp.BindJSON(&j)
    	fmt.Println(j)
}

POST

import "github.com/zzfup/go-fetch"
import "fmt"

// header can be just like this
var headers = map[string]string{
	"Accept":       "application/json, text/plain, */*",
	"Content-Type": "application/json",
}

func main(){
    	url := "https://www.example.com"

    	payload, err := json.Marshal(a) // a can be a struct or a map

    	options := fetch.Options{
		Method: "POST",
        	// Header: headers,
        	Body: payload
        	// Timeout: 2 * time.Second,
	}

    	resp, err := fetch.Fetch(url, options)
	if err != nil {
		return  err
	}

    	fmt.Println(resp.StatusCode)
    	fmt.Println(resp.ToString)
    	var j struct{
        	Test string `json:"test"`
    	}
    	err := resp.BindJSON(&j)
    	fmt.Println(j)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Options

type Options struct {
	Method    string
	Body      []byte
	Header    map[string]string
	Timeout   time.Duration
	TLSConfig *tls.Config
}

Options http client options default option

method: GET
body: nil
header: {"Accept-Encoding": "gzip,deflate", "Accept": "*/*"}
timeout: 20s

func NewDefaultOptions

func NewDefaultOptions() Options

NewDefaultOptions create a default options

type Resp

type Resp struct {
	Body       []byte
	StatusCode int
}

Resp custom http response

func Fetch

func Fetch(url string, op Options) (Resp, error)

Fetch function url: Absolute url op: Fetch options

func (Resp) BindJSON

func (resp Resp) BindJSON(s interface{}) error

BindJSON convert body to s s can be a map or a struct

func (Resp) ToString

func (resp Resp) ToString() string

ToString convert response body to string

Jump to

Keyboard shortcuts

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