Home

Awesome

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

Timeouts and retries

Progress

File upload and download

Cache, Proxy and UNIX sockets

WebDAV

Advanced creation

Installation

To install the package, run:

go get github.com/go-zoox/fetch

Methods

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, _ := 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

Inspired By

License

GoZoox is released under the MIT License.