From 5d70e289874cbf801e187fcdbd89878042fdb6e0 Mon Sep 17 00:00:00 2001 From: Yad Smood Date: Thu, 7 Dec 2023 11:22:29 +0800 Subject: [PATCH] add doc --- utils_req.go | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/utils_req.go b/utils_req.go index 1f5589e..f66fdfd 100644 --- a/utils_req.go +++ b/utils_req.go @@ -3,6 +3,7 @@ package got import ( "bytes" "context" + "encoding/json" "io" "mime" "net/http" @@ -12,11 +13,18 @@ import ( // ReqMIME option type, it should be like ".json", "test.json", "a/b/c.jpg", etc type ReqMIME string -// Req the url. The method is the http method, default value is "GET". -// If an option is http.Header, it will be used as the request header. -// If an option is [Utils.ReqMIME], it will be used to set the Content-Type header. +// Req is a helper method to send http request. It will handle errors automatically, so you don't need to check errors. +// The method is the http method, default value is "GET". +// If an option is [http.Header], it will be used as the request header. +// If an option is [ReqMIME], it will be used to set the Content-Type header. // If an option is [context.Context], it will be used as the request context. -// Other option type will be treat as request body, it will be encoded by Utils.Write . +// Other option type will be treat as request body, it will be encoded by [Utils.Write]. +// Some request examples: +// +// Req("GET", "http://example.com") +// Req("GET", "http://example.com", context.TODO()) +// Req("POST", "http://example.com", map[string]any{"a": 1}) +// Req("POST", "http://example.com", http.Header{"Host": "example.com"}, ReqMIME(".json"), map[string]any{"a": 1}) func (ut Utils) Req(method, url string, options ...interface{}) *ResHelper { ut.Helper() @@ -66,19 +74,19 @@ type ResHelper struct { err error } -// Bytes body +// Bytes parses body as [*bytes.Buffer] and returns the result func (res *ResHelper) Bytes() *bytes.Buffer { res.ut.Helper() return res.ut.Read(res.Body) } -// String body +// String parses body as string and returns the result func (res *ResHelper) String() string { res.ut.Helper() return res.Bytes().String() } -// JSON body +// JSON parses body as json and returns the result func (res *ResHelper) JSON() (v interface{}) { res.ut.Helper() return res.ut.JSON(res.Body)