Package RQ is a lightweight REST (over HTTP/HTTPS) request library. The main goal of this library is to offer simplicity and ease of use.
go get github.com/barbosaigor/rq
e.g fetch products using Get method
var products Products
rq.Endpoint("my-api.com/products").Get().ToJSON(&products)
e.g create product using the Post method
product := Product{...}
rq.Endpoint("my-api.com/product").JSON(product).Post()
RQ has an interesting error handling if an operation within the pipeline fails, then all subsequent operations will silently forward the error, and no operations are done. For error handling, you can use Err which stores the last error entry inside the pipeline.
if rq.Endpoint("my-api.com/products").Get().Err != nil {
...
}
For more details, you can read the complete documentation on pkg.go.dev.
You can configure your requests with these methods
Endpoint defines the request endpoint (if the endpoint hasn't the protocol prefix such as http:// or https://, RQ will attach an http://)
func (rq *RQ) Endpoint(endpoint string) *RQ
JSON sets the JSON to send within the request
func (rq *RQ) JSON(data interface{}) *RQ
Text sets the text to send within the request
func (rq *RQ) Text(data string) *RQ
Cookies sets Cookies for the request
func (rq *RQ) Cookies(cookies []*http.Cookie) *RQ
Cookie sets a cookie for the request
func (rq *RQ) Cookie(cookie *http.Cookie) *RQ
SetHeader sets fields for HTTP header
func (rq *RQ) SetHeader(name, value string) *RQ
After that, you can make a request using REST verbs on these methods Post, Get, Put, Delete, Head, and Patch
func (rq *RQ) Post() *RQ
func (rq *RQ) Get() *RQ
func (rq *RQ) Put() *RQ
func (rq *RQ) Delete() *RQ
func (rq *RQ) Head() *RQ
func (rq *RQ) Patch() *RQ
After the request is done you can get the response
ToJSON unmarshal the response body to JSON
func (rq *RQ) ToJSON(data interface{}) *RQ
ToString convert the response body to string
func (rq *RQ) ToString(str *string) *RQ
StatusCode returns the status code response
func (rq *RQ) StatusCode() int
ResponseBody returns the response body as []byte
func (rq *RQ) ResponseBody() []byte
Response returns the net/http Response
func (rq *RQ) Response() *http.Response