fetch

Golang Fetch

MIT License

Stars
11

Fetch HTTP Client

Simple fetch made in Go to simplify the life of programmer.

About

Go’s http package doesn’t specify request timeouts by default, allowing services to hijack your goroutines. Always specify a custom http.Client when connecting to outside services.

Install

Default

go get github.com/rodkranz/fetch

Go DEP

dep ensure --add github.com/rodkranz/fetch

Import

import (
  "github.com/rodkranz/fetch"
)

Test

To run the project test

go test -v --cover

Example:

Simple

client := fetch.NewDefault()
response, err := client.Get("http://www.google.com/", nil)

Custom Headers

opt := fetch.Options{
    Header: http.Header{
        "Content-Type": []string{"application/json"},
        "User-Agent":   []string{"XPTO-Agent-user"},
    },
}

f := fetch.New(&opt)
rsp, err := f.GetWithContext(context.Background(), "http://www.google.com", nil)

Simple JSON POST

login := map[string]interface{}{
	"username": "rodkranz",
	"password": "loremIpsum",
}
response, err := fetch.NewDefault().
		IsJSON().
		Post("http://www.google.com/", fetch.NewReader(login))