Skip to content
/ fetch Public

Go Fetch - A Powerful, Lightweight, Easy Http Client, inspired by Web Fetch API

License

Notifications You must be signed in to change notification settings

go-zoox/fetch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Fetch - HTTP Client

HTTP Client for Go, inspired by the Fetch API, and Axios + Got (Sindre Sorhus).

PkgGoDev Build Status Go Report Card Coverage Status GitHub issues Release

Features

Main API

  • Make HTTP requests
  • Easy JSON Response
  • GZip support
    • Decode GZip response
    • Encode GZip request (Upload File with GZip)
  • HTTP/2 support
  • TLS
    • Custom TLS Ca Certificate (Self signed certificate) Example
      • Custom Client Cert and Key for two-way authentication (Client Cert and Key)
  • Simple Auth Methods
    • Basic Auth
    • Bearer Auth
  • Support cancel (using context)

Timeouts and retries

  • Support timeout
  • Support retry on failure

Progress

  • Support progress and progress events

File upload and download

  • Download files easily
  • Upload files easily

Cache, Proxy and UNIX sockets

WebDAV

  • WebDAV protocol support

Advanced creation

  • Plugin system
  • Middleware system

Installation

To install the package, run:

go get github.com/go-zoox/fetch

Methods

  • GET
  • POST
  • PUT
  • PATCH
  • DELETE
  • HEAD
  • OPTIONS
  • TRACE
  • CONNECT

Getting Started

package main

import (
  "github.com/go-zoox/fetch"
)

func main() {
  response, _ := fetch.Get("https://httpbin.zcorky.com/get")
  url := response.Get("url")
  method := response.Get("method")

  fmt.Println(url, method)
}

Examples

Get

package main

import (
  "github.com/go-zoox/fetch"
)

func main() {
  response, err := fetch.Get("https://httpbin.zcorky.com/get")
  if err != nil {
		panic(err)
	}

  fmt.Println(response.JSON())
}

Post

package main

import (
  "github.com/go-zoox/fetch"
)

func main() {
  response, err := fetch.Post("https://httpbin.zcorky.com/post", &fetch.Config{
		Body: map[string]interface{}{
			"foo":     "bar",
			"foo2":    "bar2",
			"number":  1,
			"boolean": true,
			"array": []string{
				"foo3",
				"bar3",
			},
			"nest": map[string]string{
				"foo4": "bar4",
			},
		},
	})
	if err != nil {
		panic(err)
	}

  fmt.Println(response.JSON())
}

Put

package main

import (
  "github.com/go-zoox/fetch"
)

func main() {
	response, err := fetch.Put("https://httpbin.zcorky.com/put", &fetch.Config{
		Body: map[string]interface{}{
			"foo":     "bar",
			"foo2":    "bar2",
			"number":  1,
			"boolean": true,
			"array": []string{
				"foo3",
				"bar3",
			},
			"nest": map[string]string{
				"foo4": "bar4",
			},
		},
	})
  if err != nil {
		panic(err)
	}


  fmt.Println(response.JSON())
}

Delete

package main

import (
  "github.com/go-zoox/fetch"
)

func main() {
	response, err := fetch.Delete("https://httpbin.zcorky.com/Delete", &fetch.Config{
		Body: map[string]interface{}{
			"foo":     "bar",
			"foo2":    "bar2",
			"number":  1,
			"boolean": true,
			"array": []string{
				"foo3",
				"bar3",
			},
			"nest": map[string]string{
				"foo4": "bar4",
			},
		},
	})
	if err != nil {
		panic(err)
	}

  fmt.Println(response.JSON())
}

Timeout

package main

import (
  "github.com/go-zoox/fetch"
)

func main() {
  response, err := fetch.Get("https://httpbin.zcorky.com/get", &fetch.Config{
    Timeout: 5 * time.Second,
  })
  if err != nil {
		panic(err)
	}

  fmt.Println(response.JSON())
}

Proxy

package main

import (
  "github.com/go-zoox/fetch"
)

func main() {
  response, err := fetch.Get("https://httpbin.zcorky.com/ip", &fetch.Config{
    Proxy: "http://127.0.0.1:17890",
  })
  if err != nil {
		panic(err)
	}

  fmt.Println(response.JSON())
}

Basic Auth

package main

import (
  "github.com/go-zoox/fetch"
)

func main() {
  response, err := fetch.Get("https://httpbin.zcorky.com/ip", &fetch.Config{
    BasicAuth: &fetch.BasicAuth{
      Username: "foo",
      Password: "bar",
    },
  })
  if err != nil {
		panic(err)
	}

  fmt.Println(response.JSON())
}

Download

package main

import (
  "github.com/go-zoox/fetch"
)

func main() {
	response, err := fetch.Download("https://httpbin.zcorky.com/image", "/tmp/image.webp")
  if err != nil {
		panic(err)
	}
}

Upload

package main

import (
  "github.com/go-zoox/fetch"
)

func main() {
		file, _ := os.Open("go.mod")

	response, err := Upload("https://httpbin.zcorky.com/upload", file)
  if err != nil {
		panic(err)
	}

	fmt.Println(response.JSON())
}

Cancel

package main

import (
  "github.com/go-zoox/fetch"
)

func main() {
	file, _ := os.Open("go.mod")

	ctx, cancel := context.WithCancel(context.Background())

	f := fetch.New()
	f.SetBaseURL("https://httpbin.zcorky.com")
	f.SetURL("/delay/3")
	f.SetContext(ctx)

	go func() {
		_, err := f.Execute()
		fmt.Println(err)
	}()

	cancel()
}

Depencencies

  • gjson - Get JSON Whenever You Need, you don't define type first。

Inspired By

License

GoZoox is released under the MIT License.