Giter Club home page Giter Club logo

goreq's Introduction

Goreq GoDoc Go Report Card Coverage Status

Features

  • Simplifies HTTP client usage compared to net/http
  • Can't forget to close response body
  • Checks status codes by default
  • Supports context.Context
  • JSON serialization and deserialization helpers
  • No third party dependencies

Examples

Simple GET into a string

code with net/http code with requests
req, err := http.NewRequestWithContext(ctx,
	http.MethodGet, "http://example.com", nil)
if err != nil {
	// ...
}
res, err := http.DefaultClient.Do(req)
if err != nil {
	// ...
}
defer res.Body.Close()
b, err := io.ReadAll(res.Body)
if err != nil {
	// ...
}
s := string(b)
s, err := goreq.New[string]("http://example.com").
	Fetch(context.Backgroun())
fmt.Println(s)
11+ lines1 line

POST a raw body

code with net/http code with requests
body := bytes.NewReader(([]byte(`hello, world`))
req, err := http.NewRequestWithContext(ctx, http.MethodPost,
	"https://postman-echo.com/post", body)
if err != nil {
	// ...
}
req.Header.Set("Content-Type", "text/plain")
res, err := http.DefaultClient.Do(req)
if err != nil {
	// ...
}
defer res.Body.Close()
_, err := io.ReadAll(res.Body)
if err != nil {
	// ...
}
s, err := goreq.New[string]("https://postman-echo.com/post").
	BodyRaw([]byte(`hello, world`)).
    Header("Content-Type", "text/plain").
	Fetch(ctx)
12+ lines4 lines

GET a JSON object

code with net/http code with requests
var post placeholder
u, err := url.Parse("https://jsonplaceholder.typicode.com")
if err != nil {
	// ...
}
u.Path = fmt.Sprintf("/posts/%d", 1)
req, err := http.NewRequestWithContext(ctx,
	http.MethodGet, u.String(), nil)
if err != nil {
	// ...
}
res, err := http.DefaultClient.Do(req)
if err != nil {
	// ...
}
defer res.Body.Close()
b, err := io.ReadAll(res.Body)
if err != nil {
	// ...
}
err := json.Unmarshal(b, &post)
if err != nil {
	// ...
}
post, err := goreq.New[placeholder]("https://jsonplaceholder.typicode.com").IsJson().
	Path(fmt.Sprintf("/posts/%d", 1)).Fetch(ctx)
18+ lines2 lines

POST a JSON object and parse the response

req := placeholder{
	Title:  "foo",
	Body:   "baz",
	UserID: 1,
}
res, err := goreq.New[placeholder]("https://jsonplaceholder.typicode.com").
	Path("/posts").BodyJson(req).IsJson().Fetch(ctx)
// net/http equivalent left as an exercise for the reader

Set custom headers for a request

// Set headers
obj, err := goreq.New[string]("https://postman-echo.com/get").
	Headers("User-Agent", "bond/james-bond",
		"Content-Type", "secret",
		"martini", "shaken").
	Fetch(ctx)

Easily manipulate URLs and query parameters

s, err := goreq.New[string]("https://prod.example.com/get?a=1&b=2").
	Params("b", "3","c", "4").Fetch(ctx)

FAQs

In work

goreq's People

Stargazers

Pavel Grebnev avatar

Watchers

 avatar

goreq's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.