Categorygithub.com/go-zoox/fetch
modulepackage
1.8.4
Repository: https://github.com/go-zoox/fetch.git
Documentation: pkg.go.dev

# README

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.

# Functions

Create creates a new fetch with base url Specially useful for Client SDK.
CreateNamedReader creates a named reader multipart.File, that is Request.ParseMultipartForm, does not have a name so we need to create a named reader to get the file name when uploading a file with multipart/form-data.
DefaultConfig returns the default config.
DefaultUserAgent returns the default user agent.
Delete is a wrapper for the Delete method of the Client.
Download is a wrapper for the Download method of the Client.
Get is a wrapper for the Get method of the Client.
Head is a wrapper for the Head method of the Client.
New creates a fetch client.
Patch is a wrapper for the Patch method of the Client.
Post
Post is a wrapper for the Post method of the Client.
Put is a wrapper for the Put method of the Client.
Session is remembered between requests.
SetBaseURL sets the base url.
SetTimeout sets the timeout.
SetUserAgent sets the user agent.
Stream is a wrapper for the Stream method of the Client.
Upload is a wrapper for the Upload method of the Client.

# Constants

DELETE is request method DELETE.
EnvDEBUG is the DEBUG env name.
GET is request method GET.
HEAD is request method HEAD.
PATCH is request method PATCH.
POST
POST is request method POST.
PUT is request method PUT.

# Variables

BaseURL is the default base url.
ErrCannotCopyFile is the error when the file cannot be copied.
ErrCannotCreateFormFile is the error when the form file cannot be created.
ErrCannotCreateRequest is the error when the request cannot be created.
ErrCannotSendBodyWithGet is the error when the body cannot be sent with GET method.
ErrCookieEmptyKey is the error when the key is empty.
ErrInvalidBodyMultipart is the error when the body is invalid for multipart.
ErrInvalidContentType is the error when the content type is invalid.
ErrInvalidJSONBody is the error when the body is not a valid JSON.
ErrInvalidMethod is the error when the method is invalid.
ErrInvalidURLFormEncodedBody is the error when the body is invalid for url form encoded.
ErrorInvalidBody is the error when the body is invalid.
ErrReadingResponse is the error when the response cannot be read.
ErrSendingRequest is the error when the request cannot be sent.
ErrTooManyArguments is the error when the number of arguments is too many.
METHODS is the list of supported methods.
Timeout is the default timeout.
UserAgent is the default user agent.
Version is the version of this package.

# Structs

BasicAuth is the basic auth.
Config is the configuration for the fetch.
Fetch is the Fetch Client.
Progress is a progress event.
Response is the fetch response.

# Interfaces

Body is the body of the request.
DataSource defines the interface for loading data from a data source.
NamedReadCloser is a named reader.

# Type aliases

Headers is the headers of the request.
OnProgress is the progress callback.
Params is the params of the request.
Query is the query of the request.