10000 GitHub - chyroc/requests
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

chyroc/requests

Repository files navigation

requests

Install

go get github.com/chyroc/requests

Usage

sample usage

package main

import (
	"fmt"
	"net/http"
	"time"

	"github.com/chyroc/requests"
)

func Example_Method() {
	r := requests.Get("https://httpbin.org/get")

	r = requests.Post("https://httpbin.org/post")

	r = requests.Delete("https://httpbin.org/delete")

	r = requests.New(http.MethodPut, "https://httpbin.org/put")

	fmt.Println(r.Text().Unpack())
}

func Example_unmarshal() {
	r := requests.
788F
Post("https://httpbin.org/post")

	type Response struct {
		// ...
	}

	resp := requests.JSON[Response](r)

	fmt.Println(resp.Unpack())
}

func Example_factory() {
	// I hope to set fixed parameters every time I initiate a request
	// Then, every request created by this factory will not log
	opt := requests.Options(
		requests.WithLogger(requests.DiscardLogger()),
		requests.WithTimeout(time.Second*10),
		requests.WithQuery("query", "value"),
		requests.WithHeader("Auth", "hey"),
	)

	// Send sample request
	r := requests.Get("https://httpbin.org/get", opt...)

	r = requests.Post("https://httpbin.org/get").
		WithLogger(requests.DiscardLogger()).
		WithTimeout(time.Second * 10)
	fmt.Println(r.Text().Unpack())
}

func Example_newSession() {
	session := requests.NewSession("/tmp/requests-session.txt")
	r := session.Get("https://jsonplaceholder.typicode.com/todos/1").
		WithTimeout(time.Second * 10)
	fmt.Println(r.Text().Unpack())
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

0