Documentation ¶
Overview ¶
RestReq is a wrapper around standard Go net/http client. In a simple call you can use json encoding, add headers and parse result. This should be sufficient in most use cases.
Examples ¶
- Simplest use
resp, err := restreq.New("http://example.com").Post()
- You can add a header
resp, err := restreq.New("http://example.com"). AddHeader("X-TOKEN", authToken). Post()
- Use map with JSON payload
p := map[string]any{ "string": "string", "bool": true, "float": 2.34, } resp, err := restreq.New("http://example.com"). SetContentTypeJSON(). SetJSONPayload(p). Post()
- JSON payload with KV
resp, err := restreq.New("http://example.com"). SetContentTypeJSON(). SetUserAgent("Client 1.0"). AddJSONKeyValue("string", "string"). AddJSONKeyValue("bool", true). AddJSONKeyValue("float", 2.34). Post()
Parsing response ¶
- In the default behavior, the body of the response is copied to Response.Body, and you don't have to call http.Response.Body.Close()
resp, err := restreq.New("http://example.com").Post() if err == nil { fmt.Printf("%s\n", resp.Body) }
- Default behavior is convenient but not optimal, due to redundant copying. If you need high performance, you can disable this behavior and direct access to the io.Reader. Don't forget to call Response.Body.Close()
resp, err := restreq.New("http://example.com"). WithBodyReader(). Post() if err == nil { defer resp.Response.Body.Close() b := bytes.NewBuffer([]byte{}) b.ReadFrom(resp.Response.Body) fmt.Println(b.String()) }
- Get header value
value := resp.Header("token")
- Decode JSON
s := struct { Message string `json:"message,omitempty"` }{} err := resp.DecodeJSON(&s)
Index ¶
- type DebugFlag
- type Request
- func (r *Request) AddCookie(c *http.Cookie) requester
- func (r *Request) AddHeader(k string, v string) requester
- func (r *Request) AddJSONKeyValue(key string, value any) requester
- func (r *Request) Context(ctx context.Context) requester
- func (r *Request) Debug(logger *log.Logger, flags DebugFlag) requester
- func (r *Request) Delete() (*Response, error)
- func (r *Request) Get() (*Response, error)
- func (r *Request) Patch() (*Response, error)
- func (r *Request) Post() (*Response, error)
- func (r *Request) Put() (*Response, error)
- func (r *Request) SetBasicAuth(username, password string) requester
- func (r *Request) SetContentType(s string) requester
- func (r *Request) SetContentTypeJSON() requester
- func (r *Request) SetHTTPClient(c httpClient) requester
- func (r *Request) SetJSONPayload(p any) requester
- func (r *Request) SetTimeoutSec(t int) requester
- func (r *Request) SetUserAgent(s string) requester
- func (r *Request) WithBodyReader() requester
- type Response
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Request ¶
type Request struct {
// contains filtered or unexported fields
}
Request contains all methods to operate on REST API
func (*Request) AddJSONKeyValue ¶
AddJSONKeyValue converts KV to json byte array. You can add many KV, they will be added to the map and converted to an byte array when the request is sent.
func (*Request) Debug ¶
Debug sets logger and debug flags. You can combine flags, ReqBody+ReqHeader etc.
func (*Request) SetBasicAuth ¶
SetBasicAuth sets basic auth with username and password.
func (*Request) SetContentType ¶
SetContentType sets Content-Type.
func (*Request) SetContentTypeJSON ¶
func (r *Request) SetContentTypeJSON() requester
SetContentTypeJSON sets Content-Type to application/json.
func (*Request) SetHTTPClient ¶
func (r *Request) SetHTTPClient(c httpClient) requester
SetHTTPClient sets external http client.
func (*Request) SetJSONPayload ¶
SetJSONPayload encodes map or struct to json byte array.
func (*Request) SetTimeoutSec ¶
SetTimeoutSec sets connection timeout.
func (*Request) SetUserAgent ¶
SetUserAgent sets User-Agent header.
func (*Request) WithBodyReader ¶ added in v0.1.0
func (r *Request) WithBodyReader() requester
WithBodyReader allows direct reading from http.Response.Body without copying to restreq.Response.Body