A library to facilitate the creation of http requests
Use go get command to retrieve the package (Go ^1.19
is required):
go get -u github.com/RafaelYon/requestfy
- Import it in your code:
import "github.com/RafaelYon/requestfy"
- Set up a HTTP Client:
client := requestfy.NewClient(
requestfy.ConfigDefault(),
requestfy.ConfigBaseURL("https://swapi.dev/api/")
)
- Make a request:
res, err := client.Request().Get("people/1/")
When starting the client with the default configuration (requestfy.ConfigDefault()
) json.Decoder is used.
To replace json.Decoder
with another implementation it is necessary to specify the decoder constructor during the creation of the client with the option ConfigJsonDecoder
.
To use go-json just "teach" the client how to build the new decoder:
import "github.com/goccy/go-json"
func main() {
client := client := requestfy.NewClient(
requestfy.ConfigDefault(), // Using "ConfigDefault" is optional and its settings may be overwritten by subsequent settings
requestfy.ConfigJsonDecoder(func(r io.Reader) requestfy.Decoder {
return json.NewDecoder(r)
})
)
}